[英]Convenient way to plot mean and sd per factor (and level)?
Is there an easy way to add to a geom_point()
plot the mean
plus the sd
like this here:有没有一种简单的方法可以像这样在
geom_point()
plot 中添加mean
和sd
:
Going further it would be cool to also take into account levels of a factor.更进一步,考虑一个因素的水平会很酷。 My data looks like this:
我的数据如下所示:
str(df)
'data.frame': 138 obs. of 7 variables:
$ Measurement_type: Factor w/ 3 levels "block_w_same_oil",..: 2 2 2 2 2 2 2 2 2 2 ...
$ BDV : num 45.2 64 77 70.2 67.9 55.7 59.8 67.4 75.1 75.2 ...
$ Temp : Factor w/ 2 levels "cold","warm": 1 1 1 1 1 1 1 1 1 1 ...
$ Temp_C : num 20.1 20.1 20.1 20.1 20.1 20.1 20.1 20.5 20.5 20.5 ...
$ Pollution : Factor w/ 2 levels "clean","polluted": 1 1 1 1 1 1 1 1 1 1 ...
$ Step : num 1 2 3 4 5 6 1 2 3 4 ...
$ Rep : Factor w/ 5 levels "M1","M2","M3",..: 1 1 1 1 1 1 2 2 2 2 ...
I would like to be able to create such plots easily for eg the factor Measurement_type
and Rep
.我希望能够轻松地为因子
Measurement_type
和Rep
创建这样的图。 But maybe also for Pollution
and Temp
.但也许也适用于
Pollution
和Temp
。 Is there a built-in feature so I don't have to calculate any means, sd and merge data frames on my own?是否有内置功能,因此我不必自己计算任何均值、sd 和合并数据帧?
What I have atm is:我的自动取款机是:
df %>%
ggplot(aes(x = Step, y = BDV, colour = Measurement_type, shape = Rep), alpha = 0.8) +
geom_point(aes(colour = Measurement_type), size = 3) +
stat_summary(fun.data = 'mean_sdl', geom = 'smooth') +
xlab("Step") + ylab("BDV / kV") +
theme_tq()
which produces产生
which actually does the job but is not really usable as the visualization is not great (plus the sds like in geom_ribbon are not even there, yet).它实际上完成了工作但并不是真正可用,因为可视化不是很好(加上 geom_ribbon 中的 sds 甚至还没有)。
One option would be to use two stat_summary
layers to add the mean line and the confidence bands.一种选择是使用两个
stat_summary
层来添加平均线和置信带。 If you want lines and ribbons for interaction of Rep
and Measurement_type
then drop the group
aes.如果您想要线条和色带用于
Rep
和Measurement_type
的交互,请删除group
aes。
Using some fake random example data:使用一些伪造的随机示例数据:
library(ggplot2)
set.seed(123)
df <- data.frame(
Measurement_type = sample(LETTERS[1:3], 100, replace = TRUE),
Rep = sample(letters[1:5], 100, replace = TRUE),
Step = sample(seq(5), 100, replace = TRUE),
BDV = runif(100, 25, 75)
)
ggplot(df, aes(x = Step, y = BDV, colour = Measurement_type, shape = Rep), alpha = 0.8) +
stat_summary(aes(
fill = Measurement_type,
group = Measurement_type
), fun.data = "mean_se", geom = "ribbon", alpha = .3, color = NA) +
stat_summary(aes(group = Measurement_type), fun.data = "mean_se", geom = "line") +
geom_point(size = 3) +
xlab("Step") +
ylab("BDV / kV")
EDIT编辑
ggplot(df, aes(x = Step, y = BDV, shape = Rep), alpha = 0.8) +
stat_summary(aes(
fill = Measurement_type,
group = Measurement_type
), fun.data = "mean_se", geom = "ribbon", alpha = .3, color = NA) +
stat_summary(aes(
fill = Rep,
group = Rep
), fun.data = "mean_se", geom = "ribbon", alpha = .3, color = NA) +
stat_summary(aes(colour = Measurement_type, group = Measurement_type), fun.data = "mean_se", geom = "line") +
stat_summary(aes(colour = Rep, group = Rep), fun.data = "mean_se", geom = "line") +
geom_point(aes(colour = Measurement_type), size = 3) +
xlab("Step") +
ylab("BDV / kV")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.