Error messages

ggplot2 3.4.0

ggplot2
ggplot2 3.4.0 has improved error messages when running plots.
Published

November 4, 2022

Install ggplot2 3.4.0 with:

pak::pak("cran/ggplot2@3.4.0")

Load the package with:

Better error messages

…with better wording + cli package’s rich text formatting!

library(ggplot2)

ggplot(mtcars) |> 
  geom_point(aes(mpg, disp))
Error in `geom_point()`:
! `mapping` must be created by `aes()`
ℹ Did you use `%>%` or `|>` instead of `+`?

ggplot2 error messages can be hard to diagnose because you get the error when you print the plot rather than when it happens in the code. This version tells you what the problem is, where it came from, and what to do about it.

library(ggplot2)
library(dplyr)

mtcars |> 
  ggplot(aes(x = cyl, y = hp)) +
  geom_bar()
Error in `geom_bar()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_count()` must only have an x or y aesthetic.

Learn more