我是R的新手,我有一个情节,显示所有县的每个州的percprof统计数据,但是当我试图为每个情节添加一个平均值时,它不起作用:
library(ggplot2)
data(midwest)
percprof_mean <- sd(midwest$percprof)
ggplot(midwest, aes(x=percprof, y=..density..))
+ geom_histogram(binwidth = 0.5, color = "white") + facet_grid(state ~.)
+ stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
答案 0 :(得分:0)
plyr
的解决方案。
library(ggplot2)
library(plyr)
data(midwest)
nn <- ddply(midwest, "state", transform,
state_mean = mean(percprof))
ggplot(nn) +
geom_histogram(aes(percprof, y=..density..),binwidth = 0.5, color = "white") +
geom_vline(aes(xintercept = state_mean),data=nn,linetype = 5) + facet_grid(state~.)