How to Plot a Smooth Line using GGPlot2



How to Plot a Smooth Line using GGPlot2

You will learn how to plot smooth line using ggplot2.



Contents:

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Prerequisites

  • Load the ggplot2 package and set the default theme to theme_minimal():
library(ggplot2)
theme_set(
  theme_bw() +
    theme(legend.position = "top")
  )
  • Demo dataset:
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10

Key R function: geom_smooth()

  • Key R function: geom_smooth() for adding smoothed conditional means / regression line.
  • Key arguments:
    • color, size and linetype: Change the line color, size and type.
    • fill: Change the fill color of the confidence region.

A simplified format of the function `geom_smooth():

geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95)
  • method : smoothing method to be used. Possible values are lm, glm, gam, loess, rlm.
    • method = “loess”: This is the default value for small number of observations. It computes a smooth local regression. You can read more about loess using the R code ?loess.
    • method =“lm”: It fits a linear model. Note that, it’s also possible to indicate the formula as formula = y ~ poly(x, 3) to specify a degree 3 polynomial.
  • se : logical value. If TRUE, confidence interval is displayed around smooth.
  • fullrange : logical value. If TRUE, the fit spans the full range of the plot
  • level : level of confidence interval to use. Default value is 0.95

Regression line

To add a regression line on a scatter plot, the function geom_smooth() is used in combination with the argument method = lm. lm stands for linear model.

p <- ggplot(cars, aes(speed, dist)) +
  geom_point()
# Add regression line
p + geom_smooth(method = lm)
  
# loess method: local regression fitting
p + geom_smooth(method = "loess")

Loess method for local regression fitting

# loess method: local regression fitting
p + geom_smooth(method = "loess")

Polynomial interpolation

# Remove the confidence bande: se = FALSE
p + geom_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE)

Spline interpolation

spline.d <- as.data.frame(spline(cars$speed, cars$dist))
p + geom_line(data = spline.d, aes(x = x, y = y))



Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments