I have the following dataframe
df_acc_score = data.frame(group = c("con", "dem", "sch"),
cohort = c("a1", "a3"),
score = c(12, 15, 10, 16, 19, 15, 18, 20, 17, 20, 24, 19))
I would like to draw the line between the accuracy mean score in a1 and a3 for each group. For now, my code for the graph is the following
ggplot(df_acc_score, aes(x = cohort, y = score, color = group))
geom_hline(yintercept = 5, color = "grey")
stat_summary(fun = "mean",
size = 0.1)
theme_bw()
theme(strip.background = element_blank())
ylab("Mean accuracy score")
xlab("Cohort")
How shall I do to add the line between the mean score in a1 and mean score in a3 for each group?
Thanks a lot for your response
CodePudding user response:
First, your data is not complete which means your group and cohort variables have fewer values than the score. Also a regression line with only two points is not ideal. A different option to show mean points is by calculating them beforehand so you can show them with geom_point
and combine them with geom_line
. Here is a reproducible example:
library(dplyr)
library(ggplot2)
df_acc_score %>%
group_by(group, cohort) %>%
mutate(mean_cohort = mean(score)) %>%
ggplot(aes(x = cohort, y = score, color = group, group = group))
geom_hline(yintercept = 5, color = "grey")
geom_line(size = 0.1)
geom_point()
theme_bw()
theme(strip.background = element_blank())
ylab("Mean accuracy score")
xlab("Cohort")