简体   繁体   English

Ggvis条形图 - 选择颜色

[英]Ggvis bar chart - Choosing colors

I'm using the Kaggle ' train ' data set. 我使用Kaggle“ 火车 ”的数据集。

It contains 891 rows. 它包含891行。 The column I'm using is ~Survived. 我正在使用的专栏是〜Survived。 This column consists of factor values '0' and '1'. 该列由因子值'0'和'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: 结果如下:

在此输入图像描述

I would like to give the bar for values '0' a red color and the bar for the values '1' a green color. 我想给值为'0'的条形图为红色,值为'1'的条形条为绿色。

Could someone help me out? 有人可以帮帮我吗?

Thank you in advance. 先感谢您。

You're looking for ?scale_nominal . 你在找?scale_nominal Scaling is different than with ggplot2 - there are only three scales . 缩放与ggplot2不同 - 只有三个缩放

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"))

using mutate from dplyr : 使用dplyr mutate:

library(dplyr)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM