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:
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…