简体   繁体   中英

Setting Order of R Highcharter Categories

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}") 

人甲

在此处输入图像描述

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

在此处输入图像描述

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM