为 R Shiny 中的每个向下钻取级别图选择 R Highcharter 颜色

Choosing R Highcharter colours for each drilldown level graph in Rshiny

我正在 Rshiny 中创建一个与此 中的解决方案类似的向下钻取,但我有 6 个向下钻取级别,而原始问题有 3 个级别。有没有办法为每个向下钻取级别指定颜色?例如。使用引用的问题,我将能够为 1 级城市、农场和海洋、2 级公共汽车和汽车、3 级卡尔和蝾螈等指定颜色(如下面的屏幕截图所示)。这可能吗?

向下钻取级别 1

Select 1 级“城市”导致 2 级公共汽车和汽车

Select 2 级“巴士”导致 Carl 和 Newt 等。

我尝试过的:

......
highchart() %>%
      hc_xAxis(type = "category") %>%
      hc_add_series(tibbled, "column", hcaes(x = name, y = y), color = "#E4551F", "#4572A7", 
"#AA4643", "#89A54E", "#80699B", "#3D96AE") %>%
      hc_plotOptions(column = list(stacking = "normal", events = list(click = pointClickFunction)))

这没有用,它只是使用了第一个十六进制代码。当然必须有一种方法可以说“对于类别城市使用颜色“#4572A7”等”?? 请帮忙

有几种不同的方法可以做到这一点。您没有提供可重现的问题,所以我使用了数据 gapminder.

最高级别是各大洲的平均预期寿命。第二个水平是国家平均水平。第三层是各国年均预期寿命。

我使用 highcharter 函数 colorize 来创建颜色向量。我是这样组合的:

数据

library(tidyverse)
library(highcharter)
data(gapminder, package = "gapminder")

avLE = gapminder %>% 
  group_by(continent) %>% 
  mutate(aLE = mean(lifeExp)) %>% # average by continent
  ungroup() %>% group_by(country) %>% 
  mutate(caLE = mean(lifeExp)) %>% # average by year
  ungroup() %>% arrange(desc(aLE)) %>% # order by life expectancy for continents
  mutate_if(is.numeric, round, 2)  # round to 2 decimals
summary(avLE) # check it; makes sense

gapCol = avLE %>%  # set the continets in the validated avLE as ordered
  group_by(continent) %>% 
  mutate(color = colorize(continent),
         continent = ordered(continent, 
                             levels = unique(avLE$continent)))
summary(gapCol) # check it; makes sense

钻取

# make the deepest level dropdown
gapDD2 = avLE %>% 
  arrange(year) %>%  
  group_nest(continent, country, caLE) %>% # keep these variables!
  mutate(id = country,
         type = "column", 
         data = map(data, mutate, name = year, y = lifeExp,
                    color = colorize(year)), # set the color (easier with #)
         data = map(data, list_parse))

gapDD1 = avLE %>% 
  arrange(country) %>%  # arrange by country, set as ordered, then find colors
  mutate(country = ordered(country, levels = unique(country))) %>%
  mutate(color = ordered(colorize(country),     # colors/countries align
                         levels = unique(colorize(country)))) %>% 
  group_nest(continent) %>% 
  mutate(id = continent,
         type = "column", 
         data = map(data, mutate, name = country, y = caLE, 
                    color = color,  # set the color (a few more steps than with #s)
                    drilldown = country),
         data = map(data, list_parse)) 

图表

# take a look:
hchart(gapCol, "column", name = "Continental Averages",
       hcaes(x = continent, color = continent, y = aLE, 
             name = "continent", drilldown = "continent")) %>% 
  hc_drilldown(allowPointsDrillDown = T,
               series = c(list_parse(gapDD1), list_parse(gapDD2))) 



闪亮

我已经提供了一个非常简单的示例来说明如何在 Shiny 应用程序中呈现此图。在此示例中,除了调用 hchart 之外的所有代码都在设置 ui 之前调用。

ui <- fluidPage(
  fluidRow(highchartOutput("myHC"))
)
server <- function(input, output, session){
  output$myHC <- renderHighchart({
    hchart(gapCol, "column", name = "Continental Averages",
           hcaes(x = continent, color = continent, y = aLE, 
                 name = "continent", drilldown = "continent")) %>% 
      hc_drilldown(allowPointsDrillDown = T,
                   series = c(list_parse(gapDD1), list_parse(gapDD2))) 
  })
}
shinyApp(ui = ui, server = server)

如果您有任何问题,请告诉我。