Search code examples
rdplyrggvis

How to control colors assigned to data groups?


Code:

mtcars %>% group_by(cyl,am) %>% ggvis(~mpg, fill = ~factor(cyl)) %>% layer_densities()

The code above plots density graphs for each group of "cyl" and assigns different colors according to three levels of cyl: (4,6,8). But I don't have control over what color is assigned to each group.

How can I assign specific colors e.g. "red", "green", and "yellow" to each group?


Solution

  • You can add the following to your ggvis function

    ... %>% scale_ordinal("fill", range = c("red", "green", "yellow"))