I have a data frame that looks like this
presence = rep(c("yes","no"),each=3,2)
students = sample(seq(0,100,by=10), 12, replace = TRUE)
year = rep(c("2015","2016"),each=2,6)
data = data.frame(presence, students, year)
I plot my data like this
ggplot(data, aes(as.factor(presence), students, color=factor(year))) +
geom_jitter(size=4, alpha=0.4, shape=16,position = position_jitterdodge()) +
stat_summary(
aes(color = year),
fun=mean,
fun.min = function(x) mean(x) - sd(x),
fun.max = function(x) mean(x) + sd(x),
geom = "pointrange", size = 0.4,
position = position_dodge(0.8)
) +
stat_summary(fun = mean, geom="line")
What I am really struggling to do is to find a way to connect with a line the dots in reds (mean values) and the dots in blue.
Is there any other way to do it except of geom_segment and make my plot look like this?




