Home > OS >  Manual series names in highcharter
Manual series names in highcharter

Time:03-28

Looking to replicate the following ggplot logic in highcharter.

ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species))  
  geom_point()  
  scale_color_manual(values = c("red", "green", "blue"), labels = c("My custom species name", "Another species name", "Species XYZ"))

This is the closest I can do

highchart() |>
  hc_add_series(data = iris, type = "scatter", hcaes(x = Sepal.Length, y = Petal.Length, group = Species)) |>
  hc_colors(colors = c("red", "green", "blue"))

The problem is that I need to manually override the group names without altering the underlying data. I am aware that each group can be added separately using hc_add_series(), but this is not an elegant solution, especially when the number of groups is large. Wondering if there's a code logic that is similar to what is available in ggplot.

CodePudding user response:

Found the solution that was simple, but not intuitive. Apparently, the name argument in hc_add_series() can take a vector of names. It would have perhaps been more intuitive if this argument was named names instead.

highchart() |>
  hc_add_series(data = iris, type = "scatter", hcaes(x = Sepal.Length, y = Petal.Length, group = Species),
                name = c("My custom species name", "Another species name", "Species XYZ")) |>
  hc_colors(colors = c("red", "green", "blue"))
  • Related