繁体   English   中英

在 R 的 Highchart 中,如何通过条形图中的负值和正值更改 colors

[英]In Highchart in R how to change colors by negative and positive values in a bar chart

这是我的 dataframe:

    df <- structure(list(month_date = structure(c(13149, 13180, 13208, 
13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483
), class = "Date"), monthly_error = c(-228, -445, -1213, -1937, 
-687, -1437, -1275, -293, -524, 242, -100, -359), pos_neg = c("negative", 
"negative", "negative", "negative", "negative", "negative", "negative", 
"negative", "negative", "positive", "negative", "negative")), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

我想将颜色更改为“红色”为负值,“蓝色”为正值。

highchart() %>% 
  
  hc_add_series(data = df ,"column",
                color = colors[1],
                hcaes(x = month_date, y = monthly_error, color = pos_neg),
                showInLegend = TRUE)

我该如何设置?

在 ggplot 我们可以手动设置。

但是我怎样才能在 Highchart 中做到这一点?

根据您的情况调整这篇文章的答案,您可以将一列十六进制 colors 添加到您的数据中,然后可以将其映射到color aes 上,我使用htmltools:: parseCssColors将 R 颜色名称转换为十六进制代码:

library(highcharter)

df$color <- ifelse(df$pos_neg == "negative", "red", "blue")
df$color <- htmltools::parseCssColors(df$color)

highchart() %>% 
  hc_add_series(data = df, "column",
                hcaes(x = month_date, y = monthly_error, color = color),
                showInLegend = TRUE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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