Home > Enterprise >  Incorrect legend color shown on Highchart horizontal bar plot
Incorrect legend color shown on Highchart horizontal bar plot

Time:08-27

I am working on a horizontal bar plot for some visualizations using the highcharter library in R. However, I see that the legends are not shown according to the color scheme defined by the group variable.

enter image description here

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"))

Is there a way to fix this?

Data: enter image description here

CodePudding user response:

You should call the colors outside of the hcaes argument like this:

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"))

Output:

enter image description here

  • Related