Parseltongue
Parseltongue

Reputation: 11657

Add error bars to ggplot line plot with grouping variable

I am trying to create a line chart with error bars in ggplot where the x value are before and after labels, and the y value are the values for different groupings

I have the following data:

structure(list(politics = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Democrat", 
"Republican"), class = "factor"), outgroup_pairing = c("ingroup", 
"outgroup", "ingroup", "outgroup", "ingroup", "outgroup", "ingroup", 
"outgroup", "ingroup", "outgroup", "ingroup", "outgroup", "ingroup", 
"outgroup", "ingroup", "outgroup"), n = c(70L, 40L, 80L, 40L, 
70L, 40L, 80L, 40L, 70L, 40L, 80L, 40L, 70L, 40L, 80L, 40L), 
    variable = c("mean_outgroup_feelings", "mean_outgroup_feelings", 
    "mean_outgroup_feelings", "mean_outgroup_feelings", "sd_outgroup_feelings", 
    "sd_outgroup_feelings", "sd_outgroup_feelings", "sd_outgroup_feelings", 
    "mean_outgroup_feelings", "mean_outgroup_feelings", "mean_outgroup_feelings", 
    "mean_outgroup_feelings", "sd_outgroup_feelings", "sd_outgroup_feelings", 
    "sd_outgroup_feelings", "sd_outgroup_feelings"), value = c(20.0887142857143, 
    19.49375, 23.62375, 24.59675, 2.35688161103258, 3.44710671484444, 
    2.25773534858986, 3.01420267102051, 20.6534285714286, 25.6335, 
    24.34575, 31.38, 2.43592948564522, 4.15701197160308, 2.3931516332475, 
    3.24980877149442), post = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("before", 
    "after"), class = "factor"), condition = c("Democrat_ingroup_pairing", 
    "Democrat_outgroup_pairing", "Republican_ingroup_pairing", 
    "Republican_outgroup_pairing", "Democrat_ingroup_pairing", 
    "Democrat_outgroup_pairing", "Republican_ingroup_pairing", 
    "Republican_outgroup_pairing", "Democrat_ingroup_pairing", 
    "Democrat_outgroup_pairing", "Republican_ingroup_pairing", 
    "Republican_outgroup_pairing", "Democrat_ingroup_pairing", 
    "Democrat_outgroup_pairing", "Republican_ingroup_pairing", 
    "Republican_outgroup_pairing")), row.names = c(NA, -16L), vars = "politics", drop = TRUE, class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), indices = list(c(0L, 1L, 4L, 
5L, 8L, 9L, 12L, 13L), c(2L, 3L, 6L, 7L, 10L, 11L, 14L, 15L)), group_sizes = c(8L, 
8L), biggest_group_size = 8L, labels = structure(list(politics = structure(1:2, .Label = c("Democrat", 
"Republican"), class = "factor")), row.names = c(NA, -2L), vars = "politics", drop = TRUE, class = "data.frame"))

I can graph the main results like so:

ggplot(data, 
       aes(x = post, y = value, col = condition, group = condition)) + 
  geom_line() + 
  geom_point() +
  ylab("Value") +
  xlab("Before and After Measures") +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("Before and After Measurements") +
  labs(color='Pairing Type') 

I'm trying to add an error bar around those lines by first making a separate dataframe:

error_bars <- tidyr::spread(data, variable, value)

And then adding this to the ggplot chain above

geom_errorbar(data = filter(error_bars, str_detect(condition, "outgroup")), 
              mapping = aes(x = post, 
                            ymin = mean_outgroup_feelings-sd_outgroup_feelings, 
                            ymax = mean_outgroup_feelings+sd_outgroup_feelings, 
                            group = condition), 
              width=0.1, size=1, color="black") 

However, it's not working as this dataframe has no "value" object. What's the best way to go about doing this?

Upvotes: 1

Views: 564

Answers (1)

Z.Lin
Z.Lin

Reputation: 29085

You might be looking for something like this?

(I added facet_wrap for better visibility, as the lines are all rather close to one another.)

error_bars <- tidyr::spread(data, variable, value)
colnames(error_bars)[6:7] <- c("mean", "sd") # shorter variable names

ggplot(error_bars,
       aes(x = post, y = mean, color = condition, group = condition)) +
  geom_line() +
  geom_point() +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd)) +
  facet_wrap(~condition)

plot

Upvotes: 1

Related Questions