Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

r - multiple ggplot linear regression lines

I am plotting the occurrence of a species according to numerous variables on the same plot. There are many other variables but I've only kept the important ones for the sake of this post:

 > str(GH) 
 'data.frame':  288 obs. of  21 variables: 
 $ Ee       : int  2 2 1 7 6 3 0 9 3 7 ... 
 $ height   : num  14 25.5 25 21.5 18.5 36 18 31.5 28.5 19 ... 
 $ legumes  : num  0 0 55 30 0 0 55 10 30 0 ... 
 $ grass    : num  60 50 30 35 40 35 40 40 35 30 ... 
 $ forbs    : num  40 70 40 50 65 70 40 65 70 70 ... 

I've managed to plot this fine and get it looking nice using (where Ee is the species in question):

ggplot(data=GH,aes(y=y,x=x),ylab="Number of individuals (N)",xlab="Percentage cover (%); OR  Height(cm))+
geom_jitter(aes(legumes,Ee),colour="blue")+ 
geom_jitter(aes(grass,Ee),colour="green")+ 
geom_jitter(aes(forbs,Ee),colour="red")+ 
geom_jitter(aes(height,Ee),colour="black") 

However, I want to add regression lines for each of the variables (and calculate the R squared value), and have had no luck so far. Also the axes labels refuse to change from X and Y which I have never encountered before. Could anybody give me any help on this? Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using geom_smooth geom in ggplot2 gets regression lines to display. I am using mtcars data set as it's very similar to yours:

ggplot(mtcars) + 
  geom_jitter(aes(disp,mpg), colour="blue") + geom_smooth(aes(disp,mpg), method=lm, se=FALSE) +
  geom_jitter(aes(hp,mpg), colour="green") + geom_smooth(aes(hp,mpg), method=lm, se=FALSE) +
  geom_jitter(aes(qsec,mpg), colour="red") + geom_smooth(aes(qsec,mpg), method=lm, se=FALSE) +
  labs(x = "Percentage cover (%)", y = "Number of individuals (N)")

Also, I removed aes(y=y,x=x) from ggplot as it carries no meaning. The result:

enter image description here

There is more elaborate (but better looking) method to accomplish the same using melt from reshape2 package:

require(ggplot2)
require(reshape2)
mtcars2 = melt(mtcars, id.vars='mpg')
ggplot(mtcars2) +
  geom_jitter(aes(value,mpg, colour=variable),) + geom_smooth(aes(value,mpg, colour=variable), method=lm, se=FALSE) +
  facet_wrap(~variable, scales="free_x") +
  labs(x = "Percentage cover (%)", y = "Number of individuals (N)")

One important element of this solution is option scales="free_x" that allows independent scale of X across each facet plot.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...