[英]How to draw regression line R ggplot
我有以下 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))
我想在每個組的 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("Mean accuracy score")+
xlab("Cohort")
我應該如何為每個組添加 a1 中的平均分數和 a3 中的平均分數之間的線?
非常感謝您的回復
首先,您的數據不完整,這意味着您的組和隊列變量的值少於分數。 此外,只有兩點的回歸線也不理想。 顯示平均點的另一種選擇是預先計算它們,這樣您就可以使用 geom_point 顯示它們並將它們與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("Mean accuracy score")+
xlab("Cohort")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.