1

我正在尝试为 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")

并且出现了以下错误

错误:出现意外符号:
“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"

你能帮忙解决这个错误还是有另一种方法可以做到这一点?

更新信息: 我的数据样本看起来像 在此处输入图像描述

我在这些数据上运行以下代码来绘制两位面试官的均值 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: 在此处输入图像描述

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

4

1 回答 1

1

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

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

第二,我在ggplot之前做了数据分析,使用group_by,summary,然后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 包(v0.2.1)于 2019 年 3 月 4 日创建

于 2019-03-04T05:22:09.963 回答