highcharts R-Highcharter范畴的定序

lokaqttq  于 2022-11-11  发布在  Highcharts
关注(0)|答案(1)|浏览(156)

我注意到我的条形图会根据字母顺序改变顺序。我使用了选择输入,因此,如果一个人的名字以A开头,他会在顶部,但如果是C后面的字母,他会移到底部。这不是基于条形图的值,而是似乎与名字绑定在一起。我如何使ProviderName总是在顶部?
我的hc代码如下

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

bn31dyow

bn31dyow1#

我不确定原始数据是什么样的,但我将提供一个简单的示例,说明更改轴上项目顺序的一种方法。您可以通过简单地使用hc_xAxis,然后列出类别顺序来更改顺序。

library(highcharter)
library(tidyverse)

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

输出

数据

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

相关问题