Highcharter 栏颜色问题

Highcharter bar colors issue

我有以下功能:

library(highcharter)
hchart(
  newdata,
  "bar",
  hcaes(
    x = Gender,
    group = is_churner,
    y = Percent,
    label = Nominal,
    color = color
  )
) %>%
  hc_title(text = "") %>%
  hc_yAxis(
    title = list(text = ""),
    labels = list(format = "{value}")
  ) %>%
  hc_xAxis(title = list(text = "")) %>%
  hc_tooltip(
    pointFormat = "
      Percent: {point.y:,.0f}%<br>
      Nominal: {point.label:,.0f}<br>"
  )

该函数采用如下数据帧:

newdata <- data.frame(
  Gender = c(rep("Female", 2), rep("Male", 2)),
  is_churner = c("Active", "Churner", "Active", "Churner"),
  Nominal = c(7267L, 933L, 9767L, 1313L),
  Percent = c(42.7, 41.5, 57.3, 58.5), 
  color = c("green", "red", "green", "red")
) %>%
  dplyr::group_by(is_churner)

其中一列 - 颜色应该映射到 hchart 函数的颜色参数。据我记得,这是曾经有用的东西。

然而,现在我得到了一个非常丑陋的图表。

条形图的颜色与颜色列均不对应,下面的图例(颜色所代表的内容)也没有相应地着色。

我做错了什么?

我认为颜色必须在 hcaes 之外单独调用,但您的方法似乎更多 intuitive/sensible,很遗憾它不起作用。以下作品:

hchart(
  newdata,
  "bar",
  hcaes(
    x = Gender,
    group = is_churner,
    y = Percent,
    label = Nominal
  ), color = c("green", "red")
) %>%
  hc_title(text = "") %>%
  hc_yAxis(
    title = list(text = ""),
    labels = list(format = "{value}")
  ) %>%
  hc_xAxis(title = list(text = "")) %>%
  hc_tooltip(
    pointFormat = "
    Percent: {point.y:,.0f}%<br>
    Nominal: {point.label:,.0f}<br>"
  )

以下也适用:

hchart(
  newdata,
  "bar",
  hcaes(
    x = Gender,
    group = is_churner,
    y = Percent,
    label = Nominal
  )
) %>%
  hc_title(text = "") %>%
  hc_yAxis(
    title = list(text = ""),
    labels = list(format = "{value}")
  ) %>%
  hc_xAxis(title = list(text = "")) %>%
  hc_tooltip(
    pointFormat = "
    Percent: {point.y:,.0f}%<br>
    Nominal: {point.label:,.0f}<br>"
  ) %>% 
  hc_colors(c("green", "red"))

我已从 hcaes 中删除颜色并将其添加到 hc_colors

我遇到了同样的问题,在我的情况下,我的 css 文件否决了我在 hchart 颜色中指定的颜色。

当我删除 css 文件中的相关行时,颜色命令确实按照您最初指定的方式工作。不需要在hcaes.

外单独调用