ggplot2并排绘制变量的平均值和sd

时间:2019-03-03 14:00:31

标签: r ggplot2 combinedchart

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

enter image description here

蓝色条表示平均值,橙色条表示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")

并且出现了以下错误

  

错误:
中出现意外符号   “ 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()函数中,但没有用

1 个答案:

答案 0 :(得分:0)

看来position="dodge"是用于相同x的geom,而不是stat。我想出了两种解决方案。

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

第二步,我在ggplot之前进行了数据分析,使用group_by进行汇总,然后进行汇总以使数据变长。然后,既然数据已经处理完毕,我们就可以使用常规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创建