Highcharter栏颜色问题

时间:2019-02-16 22:00:12

标签: r highcharts r-highcharter

我具有以下功能:

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函数的color参数。据我记得,这曾经是可行的。

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

条形图的颜色都不对应于颜色列,下面的图例(颜色代表的颜色)也没有相应地着色。

我在做什么错?

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为必须在hcaes之外单独调用颜色,但是您的方法似乎更直观/更明智,因此感到遗憾,这是行不通的。以下作品:

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中。

答案 1 :(得分:0)

我遇到了同样的问题,就我而言,我的css文件否决了我在hchart颜色中指定的颜色。

当我删除css文件中的相关行时,color命令确实按照您最初指定的方式工作。您无需在hcaes外单独调用它。