我正在尝试制作一个 ggplot 图,它根据另一个(ZONE)使用 显示变量(SIZE)的比例face_grid,然后在每个类别中使用 显示二元变量(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())
Run Code Online (Sandbox Code Playgroud)
在此先感谢并为我的英语感到抱歉。
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())
Run Code Online (Sandbox Code Playgroud)