One common error message you may encounter when using ggplot2 in R is:
Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on
a new line?
This error occurs when you attempt to create a plot using the ggplot2 data visualization package in R, but accidently place the plus (+) sign at the beginning of a new line instead of the end of the current line.
The following example shows how to fix this error in practice.
How to Reproduce the Error
Suppose we attempt to create a scatter plot in ggplot2 using variables from the built-in mtcars dataset in R:
library(ggplot2)
#attempt to create scatter plot
ggplot(mtcars, aes(mpg, wt))
+ geom_point()
Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on
a new line?
We receive an error because we placed the plus (+) sign at the beginning of a new line.
How to Fix the Error
To fix this error, we simply need to make sure we place the plus (+) sign at the end of the first line:
library(ggplot2)
#create scatter plot
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
Notice that we’re able to successfully create a scatter plot without any errors because we moved the plus (+) sign to the end of the first line.
Additional Resources
The following tutorials explain how to troubleshoot other common errors in R:
How to Fix in R: could not find function “ggplot”
How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length