1

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?

2
  • In the main results above, you are graphing both mean & sd values. Is that what you want? Commented Apr 29, 2019 at 1:00
  • no, sorry. In the real code, I filter out those sd values. (Forgot to add it above) Commented Apr 29, 2019 at 1:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes! This is perfect. Thanks so much.
You wouldn't happen to know an easy way to facet based on regex, would you? I'd like to have both of the facets that have the word "Republican" in them to be in the same graph, and both of the graphs that have the word "Democrat" to be in the other.
facet_wrap(politics)? (since that variable is available in your dataset)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.