我正在使用ggplot2创建直方图面板,我希望能够在每个组的平均值处添加垂直线。但geom_vline()使用每个面板的相同拦截(即全球平均):为ggplot2中的每个面板添加一条具有不同截距的垂直线
require("ggplot2")
# setup some sample data
N <- 1000
cat1 <- sample(c("a","b","c"), N, replace=T)
cat2 <- sample(c("x","y","z"), N, replace=T)
val <- rnorm(N) + as.numeric(factor(cat1)) + as.numeric(factor(cat2))
df <- data.frame(cat1, cat2, val)
# draws a single histogram with vline at mean
qplot(val, data=df, geom="histogram", binwidth=0.2) +
geom_vline(xintercept=mean(val), color="red")
# draws panel of histograms with vlines at global mean
qplot(val, data=df, geom="histogram", binwidth=0.2, facets=cat1~cat2) +
geom_vline(xintercept=mean(val), color="red")
我怎样才能得到它使用每个小组的组平均为x轴截距? (如果您还可以按平均值的值添加文本标签,则可以添加文本标签。)