0

I noticed that my bar graphs change in order based on alphabetical order. I'm using a selectinput, thus if a person who is selected with a name beginning in A, they are at the top, but if it is a letter after C, then they move to the bottom. This is not based on the value of the bars, but seems tied to the names. How can I keep the ProviderName at top always?

My hc code is below

hchart(
    comparison_prov_df,
    type = "bar",
    hcaes(x = Metric, y = Value, group = ProviderName),
    colorByPoint = F,
    showInLegend = T,
    dataLabels = list(enabled = T)
  ) %>%
    hc_chart(zoomType = "xy") %>%
    hc_tooltip(crosshairs = TRUE, shared = FALSE, borderWidth = 1) %>%
    hc_credits(
      enabled = TRUE,
      text = ""
    ) %>%
    hc_add_theme(hc_theme_elementary()) %>%
    hc_legend(enabled = TRUE) %>%
    hc_exporting(
      enabled = TRUE,
      filename = "data"
    ) %>%
    hc_title(
      text = "Title",
      align = "left"
    ) %>%
    hc_yAxis(
      title = list(text = "Y Axis"),
      labels = list(
        reserveSpace = TRUE,
        overflow = "justify"
      )
    ) %>%
    hc_xAxis(title = "") %>%
    hc_tooltip(pointFormat = "{point.y:.1f}") 

Person A

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
DocProc
  • 103
  • 7
  • 1
    Could you please provide a [reproducible example](https://stackoverflow.com/q/5963269/8107362). Without any sample data it's hard to tell – mnist Oct 21 '19 at 09:28
  • Also, please add the list of all packages you are using. Similar question about ordering series in the legend: https://stackoverflow.com/questions/17997717/highcharts-change-legend-index-order If you want to update indexes "on fly", you can calculate them and use JavaScript chart.update() method to update indexes. – raf18seb Oct 21 '19 at 10:36

1 Answers1

1

I am not sure what the original data is like, but I'll provide a simple example of one way to change the order of items on an axis. You can change the order by simply using hc_xAxis and then listing the category order.

library(highcharter)
library(tidyverse)

df %>%
  hchart("bar", hcaes(x = category, y = value, group = group)) %>%
  hc_xAxis(
    categories = list(
      "Pineapples",
      "Strawberries",
      "Apples",
      "Plums",
      "Blueberries",
      "Oranges"
    )
  )

Output

enter image description here

Data

set.seed(326)

df <-
  data.frame(category = rep(c(
    "Apples", "Oranges", "Plums", "Pineapples", "Strawberries", "Blueberries"
  ), each = 2),
  value = round(runif(12, min = 0, max = 100)),
  group = rep(c(2020, 2021), 6))
AndrewGB
  • 16,126
  • 5
  • 18
  • 49