如何为每个组创建一个带有动态变量的条形图并包含错误条?

时间:2018-04-29 03:04:18

标签: r bar-chart candlestick-chart

我尝试使用R创建一个与此model graph完全相同格式的动态条形图。不幸的是,我的老师没有为我们提供模型图的R代码,因此这个动态条形图是花了我几个小时,我仍然无法使它与模型图完全相同。 我用来创建动态条形图的数据如下:

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")

此输出表的一部分图像显示在此图像中: two-way ANOVA table

有人可以教我如何使用与此模型图完全相同的格式创建动态条形图吗?谢谢。

1 个答案:

答案 0 :(得分:1)

你可以从这个情节开始。

library(tidyverse)
tidyadhd %>%
  group_by(treatment, state) %>%
  mutate(n = n()) %>%
  ungroup() %>%
  group_by(treatment, state, n) %>%
  summarise_at(vars(voltage), funs(mean, sd = sd), na.rm = TRUE) %>%
  mutate(se = sd / sqrt(n)) %>%
  ggplot(aes(treatment, mean, group = state, fill = state)) + geom_bar(stat = "identity", position = "dodge", color = "black") + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = 0.2, position = position_dodge(0.9)) +
  theme_classic() + scale_fill_manual(values = c("gray", "white")) +
  theme(
    legend.position = "bottom",
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_blank(),
    legend.title=element_blank())

enter image description here