英文:
How to draw regression line R ggplot
问题
我有以下的数据框:
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))
我想要为每个组绘制a1和a3的平均准确度分数之间的线。目前,我的图形代码如下:
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("平均准确度分数") +
xlab("队列")
我应该如何添加每个组中a1和a3的平均分数之间的线?
谢谢你的回复。
英文:
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
答案1
得分: 0
首先,您的数据不完整,这意味着您的组和队列变量的值比分数少。而且,仅有两个点的回归线并不理想。另一个显示平均分的选项是预先计算它们,这样您可以使用 geom_point
显示它们,并将它们与 geom_line
结合起来。以下是一个可重现的示例:
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("平均准确度分数")+
xlab("队列")
请注意,这只是代码的翻译部分。
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论