Highcharter酒吧颜色问题

问题描述 投票:1回答:1

我有以下功能:

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函数的颜色参数。据我所知,这是以前的工作。

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

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

我究竟做错了什么?

enter image description here

r highcharts r-highcharter
1个回答
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


0
投票

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

当我删除css文件中的相关行时,color命令以您最初指定的方式工作。你不需要在hcaes之外单独调用它。

© www.soinside.com 2019 - 2024. All rights reserved.