ggplot有多条回归线来显示随机效应

Ton*_*oni 5 plot r linear-regression ggplot2

我知道这个这个帖子.但是,当我尝试以下操作时,我似乎没有得到预期的结果:

数据可以直接从这里加载.这个想法是在一个完全虚构的数据集,葡萄糖在血液中不同种族的完成水平几个运动员将取决于一些虚构的氨基酸(AAA):

在此输入图像描述

对情节的呼吁是:

ggplot(df, aes(x = AAA, y = glucose, color=athletes)) +  
  geom_point() + geom_smooth(method="lm", fill=NA)
Run Code Online (Sandbox Code Playgroud)

而且我希望每个运动员得到不同的线,而不是单一的回归线.我的想法是得到类似的东西这个.

MLa*_*oie 7

这样的事情?

ggplot(df, aes(x = AAA, y = glucose, color=athletes, group=athletes)) +  
  geom_point() + geom_smooth(method="lm", fill=NA)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者你可能更喜欢这个

ggplot(df, aes(x = AAA, y = glucose, color=as.factor(athletes), group=as.factor(athletes))) +  
  geom_point() + geom_smooth(method="lm", fill=NA)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述