Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 248
glnvdl Avatar asked Nov 17 '25 08:11

glnvdl


1 Answers

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"))
like image 162
Rorschach Avatar answered Nov 20 '25 00:11

Rorschach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!