R Highcharter:具有条件颜色的极坐标图

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

我正在使用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
r highcharts shiny r-highcharter
1个回答
3
投票

嗨你想将colorByPoint设置为TRUE并使用ifesle来设置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

希望这可以帮助

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