5

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

user25002
  • 51
  • 1
  • 1
  • 3
  • You seem to be trying to label `ggplot` like base graphics. This won't work. Try `+ labs(x = "Percentage cover (%)", y = "Number of individuals (N)")` – Gregor Thomas Sep 09 '14 at 20:26

1 Answers1

14

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

topchef
  • 19,091
  • 9
  • 63
  • 102
  • Perfect! That's exactly what I wanted to do! Thanks. First time playing around with ggplot. Do you know how I could add the R sq. values to each of the lines? I have the values by using: Eeleg<-lm(Ee~legumes) etc but I would like to place them on the graph – user25002 Sep 09 '14 at 21:00
  • 1
    Is it possible to include the correlation coefficients, slopes, and intercepts with this approach? – philiporlando Mar 20 '18 at 00:22
  • I have tried the following code to add the equations and the R², but it didn't work: ``` stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")), label.x.npc = "right", label.y.npc = "bottom", formula = y~x, parse = TRUE, size = 2.8) ``` – Marcel Jun 21 '23 at 16:57