ggplot2 并排绘制变量的均值和标准差

ggplot2 to plot mean and sd of a variable side by side

我正在尝试为 R 中的两个不同组创建一个变量的均值和 sd(并排)图,以获得类似的结果。

其中蓝色条是均值,橙色条是 SD。

我为此使用了 R 中的 ggplot2 包。 如果我单独使用这些代码

ggplot(data, aes(x=factor(grouping variable), y=my variable)) + stat_summary(fun.y="mean", geom="bar", col="blue")

ggplot(data, aes(x=factor(grouping variable), y=my variable)) + stat_summary(fun.y="sd", geom="bar", col="orange")

它们运行良好,但在两个不同的图形中生成均值和标准差。

所以我尝试使用

将它们组合在一张图中
stat = "summary", fun.y = "mean" and stat = "summary", fun.y = "sd"

以及我得到的

ggplot(data, aes(x=factor(grouping variable)) + geom_bar(aes(y=my variable), stat = "summary", fun.y = "mean", position="dodge",col="blue") + geom_bar(aes(y=my variable), stat = "summary", fun.y = "sd", position="dodge",col="orange")

出现以下错误

Error: unexpected symbol in:
"ggplot(data, aes(x=factor(grouping variable)) + geom_bar(aes(y=my variable), stat = "summary", fun.y = "mean", position="dodge",col="blue") + geom_bar(aes(y=my variable), stat = "summary", fun.y = "sd", positi ggplot"

你能帮忙修复这个错误吗?或者还有其他方法可以解决这个问题?

更新信息: 我的数据样本看起来像 enter image description here

我 运行 这些数据的以下代码绘制了两位采访者的平均 tTTO 和 sd tTTO:

ggplot(timeTTO, aes(x=interviewer, y=tTTO)) + 
  theme_light() + 
  labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
  theme(plot.title = element_text(face = "bold")) + 
  geom_bar(stat = "summary", fun.y = "mean",width=0.25, fill = "blue") + 
  geom_bar(stat = "summary", fun.y = "sd", width=0.25,fill = "orange") 

我得到了这样的东西,其中蓝色条是均值,橙色条是 SD: enter image description here

实际上,我试过用 position="dodge" 把它放在两个 geom_bar() 函数中,它没有用

似乎 position="dodge" 适用于相同 x 的 geom,但不适用于 stat。我想出了两个解决方案。

首先,我保留了您的 stat_summary 并使用 position_nudge 手动将条形图放置在您指定的位置。请注意图例是如何不起作用的,因为没有实际的绘图数据,只有统计层。

第二个是在ggplot之前做的数据分析,使用group_by,summarize,然后gather,让数据变长。然后我们可以使用常规 geom_col 既然数据已经被处理了。

library(tidyverse)
tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>%
  ggplot(aes(x=interviewer, y=tTTO)) + 
  theme_light() + 
  labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
  theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + 
  geom_bar(stat = "summary", fun.y = "mean", position = position_nudge(x = -0.125, y = 0), width = 0.25, fill = "blue") + 
  geom_bar(stat = "summary", fun.y = "sd", position = position_nudge(x = 0.125, y = 0), width = 0.25, fill = "orange")

  # Notice that the legend does not work for stat geoms

tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>%
  group_by(interviewer) %>%
  summarize(mean(tTTO), sd(tTTO)) %>%
  gather(key = "type", value = "value", 2:3) %>%
  ggplot(aes(x=interviewer, y=value, fill=type)) + 
  theme_light() + 
  labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
  theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + 
  geom_col(position = "dodge", width = 0.25) +
  scale_fill_manual(values = c("blue","orange"))

reprex package (v0.2.1)

于 2019-03-04 创建