Ggvis histogram - color selection

I am using the Kaggle train ' dataset .

It contains 891 lines. The column I am using is ~ Survived. This column contains the values ​​of the coefficients "0" and "1".

I have plotted 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 the values ​​"0" red and the bar for the values ​​"1" green.

Can anyone help me?

Thanks in advance.

+3


source to share


2 answers


You are looking for ?scale_nominal

. Scaling is different from 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"))

      

+3


source


using mutation from dplyr

:



library(dplyr)

train %>% mutate(mycolours = ifelse(Survived == 0, "red", "green")) %>%
    ggvis(~Survived, fill := ~mycolours) %>%
    layer_bars()

      

+1


source







All Articles