gpt4 book ai didi

R高宪章: Polar graph having conditional colors

转载 作者:行者123 更新时间:2023-12-04 01:59:59 24 4
gpt4 key购买 nike

我正在使用 Highcharter 的极坐标图,想知道它是否能够根据标准更改颜色。我正在使用图表来查看预算与支出。当支出 < 预算时,我希望该列为绿色。当支出 > 预算时,我希望该列为红色。这可能吗?

对于代码中的示例:“物流”和“IT 与电信”支出将为绿色,所有其他支出类别将为红色。

答案需要是动态的,因为超出或低于计划的内容会不断变化。

下面是代码的简化版本。

library (shiny)
library (highcharter)

hc <- highchart() %>%
hc_chart(polar = TRUE) %>%
hc_title(text = "Budget vs Spending") %>%
hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
"Logistics", "Office Products", "Plant Consumables",
"Temp Labor", "Travel", "Other"),
tickmarkPlacement = "on",
lineWidth = 0) %>%
hc_yAxis(gridLineInterpolation = "polygon",
lineWidth = 0,
min = 0) %>%
hc_series(
list(
name = "Spend",
data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
pointPlacement = "on",
type = "column"
),
list(
name = "Budget",
data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
pointPlacement = "on",
type = "line"
)
)

hc

最佳答案

您想将 colorByPoint 设置为 TRUE,然后使用 ifelse 设置 colors,如下所示:

library(shiny)
library(highcharter)
library(tidyverse)
hc <- highchart() %>%
hc_chart(polar = TRUE) %>%
hc_title(text = "Budget vs Spending") %>%
hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
"Logistics", "Office Products", "Plant Consumables",
"Temp Labor", "Travel", "Other"),
tickmarkPlacement = "on",
lineWidth = 0) %>%
hc_yAxis(gridLineInterpolation = "polygon",
lineWidth = 0,
min = 0) %>%
hc_series(
list(
name = "Spend",
data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
pointPlacement = "on",
colorByPoint = TRUE,
type = "column",
colors = ifelse(c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000) > c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),"#F00","#0F0")
),
list(
name = "Budget",
data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
pointPlacement = "on",
type = "line"
)
)

hc

希望对你有帮助

关于R高宪章: Polar graph having conditional colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com