highcharts Highchart水平条形图上显示的图例颜色不正确

pcww981p  于 2022-11-10  发布在  Highcharts
关注(0)|答案(2)|浏览(157)

我正在使用R中的highcharter库处理一些可视化的水平条形图。但是,我发现图例没有根据group变量定义的配色方案显示。

library(highcharter)
library(tidyverse)

df%>% 
  group_by(course_code) %>% 
  hchart("bar", hcaes(x = course_code, y = completion_rate, 
                      color = Language, group = Language)) %>% 
  hc_add_theme(hc_theme_google()) %>% 
  hc_tooltip(pointFormat = "<b> Completion rate: </b> {point.completion_rate}") %>% 
  hc_title(text = "Course Completion Rates",
           style = list(fontSize = "15px", fontWeight = "bold")) %>% 
          hc_xAxis(title = list(text = "Courses")) %>%
          hc_yAxis(title = list(text = "Completion Rate"))

有办法解决这个问题吗?
数据:here

ecbunoof

ecbunoof1#

您将需要fill美学:

library(highcharter)
library(tidyverse)

df1%>% 
  group_by(course_code) %>% 
  hchart("bar", hcaes(x = course_code, y = completion_rate, 
                      fill = Language, group=Language)) %>% 
  hc_add_theme(hc_theme_google()) %>% 
  hc_tooltip(pointFormat = "<b> Completion rate: </b> {point.completion_rate}") %>% 
  hc_title(text = "Course Completion Rates",
           style = list(fontSize = "15px", fontWeight = "bold")) %>% 
  hc_xAxis(title = list(text = "Courses")) %>%
  hc_yAxis(title = list(text = "Completion Rate"))

f8rj6qna

f8rj6qna2#

您应该在hcaes参数之外调用colors,如下所示:

library(highcharter)
library(tidyverse)

df%>% 
  group_by(course_code) %>% 
  hchart("bar", hcaes(x = course_code, y = completion_rate, 
                      group = Language),
         color = c("yellow", "purple")) %>%
  hc_add_theme(hc_theme_google()) %>% 
  hc_tooltip(pointFormat = "<b> Completion rate: </b> {point.completion_rate}") %>% 
  hc_title(text = "Course Completion Rates",
           style = list(fontSize = "15px", fontWeight = "bold")) %>% 
  hc_xAxis(title = list(text = "Courses")) %>%
  hc_yAxis(title = list(text = "Completion Rate"))

输出量:

相关问题