Search code examples
rvisualizationggviskaggle

Ggvis bar chart - Choosing colors


I'm using the Kaggle 'train' data set.

It contains 891 rows. The column I'm using is ~Survived. This column consists of factor values '0' and '1'.

I have plotted the two values using the following lines of code:

train %>% ggvis(~Survived, fill = ~Survived) %>%
  layer_bars()

The result looks like this:

enter image description here

I would like to give the bar for values '0' a red color and the bar for the values '1' a green color.

Could someone help me out?

Thank you in advance.


Solution

  • You're looking for ?scale_nominal. Scaling is different than with ggplot2 - there are only three scales.

    library(ggvis)
    
    set.seed(0)
    dat <- data.frame(Survived = factor(sample(0:1, 50, rep=T)))
    
    dat %>% ggvis(~Survived, fill=~Survived) %>%
      layer_bars() %>%
      scale_nominal("fill", range = c("red", "green"))