繁体   English   中英

用填充填充ggplot(geom_bar)中的十分位

[英]decile in ggplot (geom_bar) with a fill

我有一个带有三个变量的数据框。 这是str()

'data.frame':   282 obs. of  2 variables:
 $ stars  : num  1 3 3.5 2 3.5 3 3.5 3 3.5 3.5 ...
 $ is_open: chr  "1" "0" "0" "1" ...

这是一个小样本:

structure(list(stars = c(1, 3, 3.5, 2, 3.5), is_open = c("1", 
"0", "0", "1", "1")), .Names = c("stars", "is_open"), row.names = c(NA, 
5L), class = "data.frame")

我想用ggplot形式用变量stars ggplot绘制ggplot ,并填充变量is_open

但是我只得到这个版本,因为我不知道如何选择decilesis_open变量

test %>% select(stars) %>% quantile(na.rm = T, c(0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)) %>% barplot()

但是我只能得到这个结果。 1]

使用ggplot不能正常工作:

ggplot(test, aes(x = stars(na.rm = T, c(0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)), fill = is_open)) + geom_bar(stat = "identity") 

Error in stars(na.rm = T, c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, : 'x' must be a matrix or a data frame

任何想法如何通过ggplot集成is_open变量? 它应该看起来像这样:

在此处输入图片说明

首先制作data.frame(正如Richard所建议的)。 然后分别绘制:

library(dplyr)
library(tidyr)
library(ggplot2)

test_quant <- test %>% 
  group_by(is_open) %>% 
  do(x = seq(0, 1, 0.1),
     quant = quantile(.$stars, seq(0, 1, 0.1))) %>% 
  unnest()

ggplot(test_quant, aes(as.factor(x), quant, fill = is_open)) + 
   geom_col(position = 'dodge')

在此处输入图片说明

暂无
暂无

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

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