Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot with multiple regression lines to show random effects

I am aware of this and this posts. However, I don't seem to get the expected result when I try the following:

The data can be loaded directly from here. The idea is that in a completely made-up data set, the levels of glucose in blood for several athletes at the completion of different races would depend on some fictitious amino acid (AAA):

enter image description here

The call for the plot was:

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

And I expected to get different lines for each one of the athletes, instead of one single regression line. My idea was to get something similar to this.

like image 650
Antoni Parellada Avatar asked Dec 24 '15 15:12

Antoni Parellada


1 Answers

something like this?

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

enter image description here

or maybe you would prefer this

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

enter image description here

like image 140
MLavoie Avatar answered Oct 13 '22 21:10

MLavoie