我正在尝试制作一个ggplot图,它使用face_grid
根据另一个变量(SIZE)显示变量(SIZE)的比例,然后在每个类别中显示二进制变量(BG)的平均值使用stat_summary
。但现在,我想加入点数(均值)并以百分比格式显示均值(接近该点)。加入我用stat_summary('arguments here',geom="line")
和其他命令尝试的点,但事实是我不知道该怎么做。
library(ggplot2)
library(scales)
set.seed(100)
data <- data.frame(ID = 1:600,
SIZE = rep(c('B','M','S'), each = 200),
ZONE = sample(c('A','B'), size = 600, replace=T),
BG = c(sample(0:1, size = 200, prob = c(1,1), replace=T),
sample(0:1, size = 200, prob = c(2,3), replace=T),
sample(0:1, size = 200, prob = c(1,2), replace=T)))
ggplot(data, aes(x = SIZE)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
facet_grid(~ZONE) +
stat_summary(aes(y = BG), fun.y=mean, colour="red", geom="point", size = 3) +
scale_y_continuous('Percent', labels = percent_format())
提前致谢,对不起我的英语。
答案 0 :(得分:3)
使用group
时始终记住geom_line
美学!
ggplot(data, aes(x = SIZE)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
facet_grid(~ZONE) +
stat_summary(aes(y = BG,group = 1), fun.y=mean, colour="red", geom="line", size = 3) +
scale_y_continuous('Percent', labels = percent_format())