如何在R中使用ggplot绘制回归线

huangapple go评论68阅读模式
英文:

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")

huangapple
  • 本文由 发表于 2023年1月9日 18:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定