0

I have a dataframe that looks like the following. In the real dataset, there are 14 dv's and 9 conditions.

dv   condition  mean  sd
dv1  1          4     1
dv1  2          5     1
dv1  3          3     2
dv2  1          3     1
dv2  2          6     2
dv2  3          4     1

I know I can graph these separately in the following way. I can create a separate dataframe for each dv and then graph it.

dv1 <- df[1:3,]
dv1_graph <- ggplot(dv1, aes(x=condition, y = mean, fill = condition)) + 
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
labs(title = "DV1") +
geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), width = 0.2, position = position_dodge(.9))

Is there a way to create a separate bar graph with error bars for each dv simultaneously?

melbez
  • 960
  • 1
  • 13
  • 36

1 Answers1

2

I think you want this:

ggplot(df, aes(condition, mean)) +
  geom_col(color = "black", position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2,
                position = position_dodge(.9)) +
  facet_wrap(~dv)

Update: For a bunch of individual plots

library(dplyr)
library(purrr)

plotter <- function(df, dv) {
  plot(ggplot(df, aes(condition, mean)) +
    geom_col(color = "black", position = position_dodge()) +
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2,
                  position = position_dodge(.9)) +
    ggtitle(dv))
}

nested_df <-
  df %>% 
  group_by(dv) %>% 
  nest()

walk2(nested_df$data, nested_df$dv, plotter)
Nick DiQuattro
  • 729
  • 4
  • 7
  • Thanks! Is there a way to get each graph to show up individually? I have a lot, so they're a little crowded. – melbez Dec 15 '18 at 22:38