簡體   English   中英

為 Rshiny 中的每個向下鑽取級別圖選擇 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 function 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

我提供了一個非常簡單的示例,說明如何在 Shiny 應用程序中呈現此 plot。 在此示例中,所有代碼(調用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)

如果您有任何問題,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM