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

时间:2017-12-21 15:42:40

标签: r highcharts shiny r-highcharter

我正在使用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

1 个答案:

答案 0 :(得分: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

希望这会有所帮助

相关问题