Search code examples
rggvis

Adjusting fill in ggvis that has mapped a variable to it


When using ggplot I can adjust the colors manually when a variable is mapped to it. Like the following example:

ggplot(mtcars, aes(cyl, mpg)) +
  geom_point(aes(col = factor(cyl))) +
  scale_color_manual(values = c('red', 'green', 'blue'))

I would like to do the same in ggvis. The only solution I found so far is hard coding the color name in the data and assigning this by fill := ~variable_name. I hope someone can tell me what to add to the following

ggvis(mtcars, ~cyl, ~mpg, fill = ~factor(cyl)) %>%
   layer_points()

Thanks a lot.


Solution

  • You can change fill colors for a categorical variable with scale_nominal. You use "fill" as the name of the property you want to control and give the colors you want via range.

    ggvis(mtcars, ~cyl, ~mpg, fill = ~factor(cyl)) %>%
        layer_points() %>%
        scale_nominal("fill", range =  c('red', 'green', 'blue'))