geom_abline

geom_abline(mapping=NULL, data=NULL, stat="abline", position="identity", ...)

Line, specified by slope and intercept

The abline geom adds a line with specified slope and intercept to the plot.

With its siblings geom_hline and geom_vline, it's useful for annotating plots. You can supply the parameters for geom_abline, intercept and slope, in two ways: either explicitly as fixed values, or stored in the data set. If you specify the fixed values (geom_abline(intercept=0, slope=1)) then the line will be the same in all panels, but if the intercept and slope are stored in the data, then can vary from panel to panel. See the examples for more ideas.

This page describes geom_abline, see layer and qplot for how to create a complete plot from individual components.

What do you think of the documentation? Please let me know by filling out this short online survey.

Aesthetics

The following aesthetics can be used with geom_abline. Aesthetics are mapped to variables in the data with the aes function: geom_abline(aes(x = var)). Note that you do not need quotes around the variable name.

Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.

Aesthetic Default Related scales
colourblackbrewer, gradient, gradient2, gradientn, grey, hue, identity, manual
size0.5identity, manual, size
linetype1identity, linetype, manual
alpha1

Layers are divided into groups by the group aesthetic. By default this is set to the interaction of all categorical variables present in the plot.

Parameters

Parameters control the appearance of the geom. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.

Returns

This function returns a layer object.

See also

Examples

> p <- qplot(wt, mpg, data = mtcars) 
>  
> # Fixed slopes and intercepts 
> p + geom_abline() # Can't see it - outside the range of the data 
  
> p + geom_abline(intercept = 20) 
  
>  
> # Calculate slope and intercept of line of best fit 
> coef(lm(mpg ~ wt, data = mtcars)) 
(Intercept)          wt 
  37.285126   -5.344472  
> p + geom_abline(intercept = 37, slope = -5) 
  
> p + geom_abline(intercept = 10, colour = "red", size = 2) 
  
>  
> # See ?stat_smooth for fitting smooth models to data 
> p + stat_smooth(method="lm", se=FALSE) 
  
>  
> # Slopes and intercepts as data 
> p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl) + geom_point() 
> df <- data.frame(a=rnorm(10, 25), b=rnorm(10, 0)) 
> p + geom_abline(aes(intercept=a, slope=b), data=df) 
  
>  
> # Slopes and intercepts from linear model 
> coefs <- ddply(mtcars, .(cyl), function(df) { 
+   m <- lm(mpg ~ wt, data=df) 
+   data.frame(a = coef(m)[1], b = coef(m)[2]) 
+ }) 
> str(coefs) 
'data.frame':	3 obs. of  3 variables:
 $ cyl: num  4 6 8
 $ a  : num  39.6 28.4 23.9
 $ b  : num  -5.65 -2.78 -2.19
> p + geom_abline(data=coefs, aes(intercept=a, slope=b)) 
  
>  
> # It's actually a bit easier to do this with stat_smooth 
> p + geom_smooth(aes(group=cyl), method="lm") 
  
> p + geom_smooth(aes(group=cyl), method="lm", fullrange=TRUE) 
  

What do you think of the documentation? Please let me know by filling out this short online survey.