我有一个包含3个变量的长格式数据集。我正在使用ggplot2绘制两个变量和另一个变分面。我也想从每个方面绘制观察的标准误差条,但我不知道如何。有人知道吗?
这是我所拥有的图片。我想在每个方面都有标准误差条。谢谢!
编辑:这是一些示例数据和情节。
data <- data.frame(rep(c("1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32"), 2),
rep(c("a","b","c","d","e","f","g","h","i","j","k","l"), 32),
rnorm(n = 384))
colnames(data) <- c("estado","sector","VA")
ggplot(data, aes(x = estado, y = VA, col = sector)) +
facet_grid(.~sector) +
geom_point()
答案 0 :(得分:2)
如果您想要的只是平均值&amp;标准误差条与每个&#34; estado&#34; - &#34;扇区&#34;组合,你可以让ggplot完成所有工作,将geom_point()
行改为stat_summary()
:
ggplot(data,
aes(x = estado, y = VA, col = sector)) +
facet_grid(. ~ sector) +
stat_summary(fun.data = mean_se)
有关该功能的更多详细信息,请参阅ggplot2包中的?mean_se
。默认参数选项为您提供高于&amp;的1个标准误差的平均值和范围。低于平均值。
如果您想显示原始点,只需添加geom_point()
行即可。 (虽然我认为这个情节对于读者而言会相当混乱,在这种情况下......)
答案 1 :(得分:1)
也许你可以试试下面的东西?
set.seed(1)
library(dplyr)
dat = data.frame(estado = factor(rep(1:32, 2)),
sector = rep(letters[1:12], 32),
VA = rnorm(384))
se = function(x) {
sd(x)/sqrt(length(x))
}
dat_sum = dat %>% group_by(estado, sector) %>%
summarise(mu = mean(VA), se = se(VA))
dat_plot = full_join(dat, dat_sum)
ggplot(dat_plot, aes(estado, y = VA, color = sector)) +
geom_jitter() +
geom_errorbar(aes(estado, y = mu, color = sector,
ymin = mu - se, ymax = mu + se)) +
facet_grid(.~sector)