如何在R中创建带有SE条的并排条形图

时间:2018-04-23 04:09:55

标签: r graph

我正在使用R studio中双向ANOVA表中的标准误差条创建一个并排条形图。条形图的完整版本应如下所示:

example bar chart

我们正在使用的数据是在Excel中:

Raw Data

并简单地以文本形式:

目前我在R studio中创建了一个表格,但我不知道下一步该做什么以及如何计算平均值,标准误差等。我创建的表格的一部分是在此链接:two-way ANOVA table

这是我在R studio中的表格:

dput(tidyadhd)

structure(list(voltage = c(9.5, 10, 9.2, 6, 9, 11, 9, 9.2, 8, 
9, 11, 9, 8.2, 8, 9, 9, 6, NA, 9.5, 9, 7.1, 9.3, 9, 9.5, 9, 9.2, 
9.3, 9, 8.5, 9, 9.2, 9.3, 8.2, 9, 9.3, NA, 9.5, 9, 9.2, 9.3,    
9, 9.5, 9, 9.2, 9.3, 9, 9.5, 9, 9, 9, 9.3, 8, 7, 8, 5.1, 4.3, 
4.5, 5.3, 4.5, 5.1, 4.3, 4.5, 5.3, 4.5, 5.1, 4.3, 4.3, 4.3, 5.3, 
6.4, 5, 8), state = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Rest", "Cue"), class = "factor"), treatment = 
c("placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "placebo", 
"placebo", "placebo", "placebo", "placebo", "placebo", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin", "ritalin", 
"ritalin", "ritalin", "ritalin", "ritalin", "ritalin"), patient = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L)), .Names = c("voltage", "state", "treatment", "patient"
), row.names = c(NA, -72L), class = "data.frame")

有人可以帮助我创建上表中的条形图吗?因为我自己试图超过24小时这样做,它只是一直告诉我错误。非常感谢你!

1 个答案:

答案 0 :(得分:0)

以下是使用ggplot2的一些想法。我建议花些时间学习如何使用这个软件包 - start here

第一步是决定如何对数据中的变量进行分组。我假设voltage是因变量,并且您对treatment对两个state中的每一个的影响感兴趣。因此,您希望绘制电压与状态的关系,并通过治疗来设计facet

您可以预处理数据以计算均值和标准误差,也可以使用stat_summary为您执行此操作。您可以按状态对条形进行着色,但我不认为这是必要的,因为轴标签是清晰的。

library(ggplot2)
theme_set(theme_bw())

ggplot(tidyadhd, aes(state, voltage)) + 
  stat_summary(fun.y = "mean", geom = "bar", fill = "grey70") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.4) +
  facet_wrap(~treatment)

enter image description here

这些所谓的“炸药地块”经常受到批评(example),建议将箱形图作为更好的选择:

ggplot(tidyadhd, aes(state, voltage)) + 
  geom_boxplot() +
  facet_wrap(~treatment)

enter image description here

另一种方法是使用geom_jitter显示各个数据点。这在这里工作,因为我们没有太多的观察;在这种情况下,它可能有助于通过state为点着色。如果您不需要图例中的颜色,请使用guides(),如图所示。

您也可以将stat_summary添加到此图中,以显示带有误差线的方法。

ggplot(tidyadhd, aes(state, voltage)) + 
  geom_jitter(width = 0.2, size = 2, aes(color = state)) +
  facet_wrap(~treatment) + 
  guides(color = FALSE)

enter image description here