【问题标题】:R Highcharter: Polar graph having conditional colorsR Highcharter:具有条件颜色的极坐标图
【发布时间】:2018-06-04 07:50:26
【问题描述】:

我正在使用 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 JS 中,我会将负责更新的代码放在 chart.events.render 属性中:jsfiddle.net/kkulig/88mb6mq8 我使用 redrawEnabled 标志来防止无限递归循环是由于在此事件中重绘图表引起的(render 自己调用 redraw 事件)。

标签: r highcharts shiny r-highcharter


【解决方案1】:

您想将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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2012-08-15
    • 1970-01-01
    • 2022-07-25
    • 2016-08-05
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多