I have a grouped bar plot (two conditions with two groups in each). I am not having trouble creating the group bar plots, but I am interested in putting individual data points on the plot and connecting them with a line. More specifically, the lines should be connected within conditions, so the data point from condition 1 group 1 connects to the corresponding data point in condition 1 group 2. I cannot find a way to do this without using facet_wrap.
I have seen facet_wrap being suggested here, but it affects the alignment of the plots when using patchwork specifically. I have provided some code for the bar plots and the code to produce the facet_wrap plot.
I am open to any suggestions which do not use facet_wrap. I was hoping there would be a way to do it directly using geom_line(aes(subject)), but that does not seem to be grouping correctly.
Code
df <- data.frame(
Subject = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3),
Condition = c(“A”, “A”, “B”, “B”, “A”, “A”, “B”, “B”, “A”, “A”, “B”, “B”),
Group = c(“group1”, “group2", “group1”, “group2",
“group1”, “group2", “group1”, “group2",
“group1”, “group2", “group1”, “group2"),
Value = c(10, 7, 8, 9, 11, 12, 13, 2, 2, 3, 4, 5))
Desired Look (excluding the connected data points)
ggplot(df, aes(x=Condition, y=Value, fill=Group)) +
stat_summary(fun.data = mean_sdl, geom = ‘bar’, position = ‘dodge’) +
stat_summary(fun.data = mean_se, geom = ‘errorbar’, width = 0.2,
position = position_dodge(width = 0.9)) +
theme_minimal()

Connected Data Points (using undesired facet)
ggplot(df, aes(x=Group, y=Value, fill=Group)) +
stat_summary(fun.data = mean_sdl, geom = ‘bar’, position = ‘dodge’) +
stat_summary(fun.data = mean_se, geom = ‘errorbar’, width = 0.2,
position = position_dodge(width = 0.9)) +
theme_minimal() +
geom_line(aes(group = Subject), alpha = 0.2, position = position_nudge(c(.14, -.14))) +
facet_wrap(~Condition)

Here is an example of how facet affects the alignment in patchwork (image on the right). Because it assumes there should be an axis title everything is misaligned.

Your group aesthetic in geom_line() needs to incorporate both the Subject and Condition information for this to work without faceting. Try the following solution (I have also inverted the signs in the position_nudge() call, since as they were they were inverting your data points, and have added a parameter nudgeAmount to easily control the nudge and a geom_point() call so you can see where the actual data points are):
nudgeAmount <- 0.14
ggplot(df, aes(x = Condition, y = Value, fill = Group)) +
stat_summary(fun.data = mean_sdl, geom = "bar", position = "dodge") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2, position = position_dodge(width = 0.9)) +
geom_point(shape = 21, position = position_nudge(nudgeAmount*c(-1,1))) +
geom_line(aes(group = paste(Subject,Condition)), alpha = 0.2, position = position_nudge(nudgeAmount*c(-1,1))) +
theme_minimal()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With