ggplot2 Error in R: Mapping should be created with `aes()` or `aes_()`. (2 Examples)
In this tutorial, I’ll demonstrate how to debug the ggplot2 error message “Error: Mapping should be created with `aes()` or `aes_()`.” in the R programming language.
Setting up the Examples
data(iris) # Load iris data to R head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load iris data to R head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2") |
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
Example 1: Replicating the Error Message – Mapping should be created with `aes()` or `aes_()`.
ggplot(iris, # Parentheses at wrong position aes(x = iris$Sepal.Length, y = iris$Sepal.Width) + geom_line()) # Error: Mapping should be created with aes() or aes_(). |
ggplot(iris, # Parentheses at wrong position aes(x = iris$Sepal.Length, y = iris$Sepal.Width) + geom_line()) # Error: Mapping should be created with aes() or aes_().
Example 2: Fixing the Error Message – Mapping should be created with `aes()` or `aes_()`.
ggplot(iris, # Parentheses at correct position aes(x = Sepal.Length, y = Sepal.Width)) + geom_line() |
ggplot(iris, # Parentheses at correct position aes(x = Sepal.Length, y = Sepal.Width)) + geom_line()
Related Articles
Have a look at the following tutorials. They illustrate topics such as coding errors and ggplot2: