ggplot2::geom_smooth()
get_help()
docs
The geom_smooth()
function is part of the {ggplot2}
package, which is part of the {tidyverse}
.
The geom_smooth()
function is used to add a trendline to a plot, commonly to a scatterplot.
x
: The x-axis coordinates to build the trendline fromy
: The y-axis coordinates to build the trendline fromcolor
(colour
): The color of the trendlinefill
: The fill of the confidence interval bandse
: Whether to include a confidence interval band in the trendline (specify TRUE
or FALSE
)method
: Which statistical formula to use to create the trendline. To specify a linear trend line, use method = "lm"
, where “lm” stands for linear model. {ggplot2}
will always print a message telling you which statistical formula was used.To use this function, you need to either first load the {ggplot2}
library, or always use the function with ggplot2::geom_smooth()
notation.
# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::geom_smooth() ggplot2
The examples below use the msleep
dataset. Learn more about this dataset with get_help("msleep")
.
# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Owl … Aotus omni Prim… <NA> 17 1.8 NA 7 0.0155 0.48
## 2 Moun… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 NA 1.35
## 3 Grea… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029 0.019
## 4 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423 600
## 5 Thre… Brad… herbi Pilo… <NA> 14.4 2.2 0.767 9.6 NA 3.85
## 6 Nort… Call… carni Carn… vu 8.7 1.4 0.383 15.3 NA 20.5
# Scatterplot of awake against sleep_rem ("y against x") with a linear model trendline
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
## CAUTION! If you forget the argument `method = "lm"`, you will get a "loess trendline,"
# This is only a problem if you do not want a loess trendline
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Scatterplot of awake against sleep_rem with custom linear trendline color and fill
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
geom_smooth(method = "lm",
color = "goldenrod",
fill = "blue")
## `geom_smooth()` using formula 'y ~ x'
# Scatterplot of awake against sleep_rem with confidence interval turned off
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
geom_smooth(method = "lm",
se=FALSE)
## `geom_smooth()` using formula 'y ~ x'
# Points are colored by vore, specified in the shared `aes()`.
# geom_smooth inherits this aesthetic to create separate trendlines for each vore
ggplot(msleep) +
aes(x = sleep_rem, y = awake, color = vore) +
geom_point() +
geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
# Points are colored by vore, specified in `geom_point()` only.
# geom_smooth will NOT inherit this aesthetic, and it creates a single trendline
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point(aes(color = vore)) +
geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'