When you are using the ggplot function, you will get the error: mapping must be created by ‘aes’ error message if you do not set up the aes function in the mapping argument properly. The ggplot error mapg can be a little tricky because you are usually using it with other functions. To fix this problem you need to make sure that the aes function is set up properly.
Description of the R Error
This error message occurs when you are using the ggplot function from the ggplot2 package with the geom box plot function. It occurs if you use the box plot function without specifically equating the aes function to the mapping argument. This is because the box plot function has multiple optional arguments such as keys, aesthetic, geom abline, axis ticks, axis limits, ylim, and other limits. All this allows you to customize your box plot. However, there is no default mapping, and getting the arguments wrong will turn the results into hash. Furthermore, it complicates the error mapg making it non-intuitive. Fortunately, it turns out to be an extremely easy error to fix.
Explanation of the R Error
Here is an example of some r code that produces this error message.
> library(ggplot2)
> df = data.frame(y=c(2, 3, 3, 4, 5, 5, 6, 8, 8, 9),
+ x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
>
> ggplot() + geom_boxplot(df, aes(x))
Error: `mapping` must be created by `aes()`
Run `rlang::last_error()` to see where the error occurred.
In this example, the data frame is fine but the aes function is not equated to the mapping argument. This causes the function to not know which argument is being referred to. The result is our error message.
How to fix the R Error
Here we have three examples, they show two ways of fixing this error. The third illustrates how the ggplot function handles the presence of missing values in the data frame.
> library(ggplot2)
> df = data.frame(y=c(2, 3, 3, 4, 5, 5, 6, 8, 8, 9),
+ x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
>
> ggplot() + geom_boxplot(df, mapping=aes(x))
In this example we simply equate the aes function to the mapping argument. This is the simplest and most obvious way to fix this error.
> library(ggplot2)
> df = data.frame(y=c(2, 3, 3, 4, 5, 5, 6, 8, 8, 9),
+ x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
>
> ggplot(df, aes(x)) + geom_boxplot()
The alternative solution is to move the arguments over to the ggplot function. In this case, you do not have to explicitly mention the mapping argument. This fixes the error by eliminating the cause.
> library(ggplot2)
> df = data.frame(y=c(2, 3, 3, 4, NA, 5, 6, 8, 8, 9),
+ x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
>
> ggplot(df, aes(x)) + geom_boxplot()
In this final example, we illustrate how the ggplot function handles missing values. Essentially it just ignores them by not plotting that data pair. This means that you do not have to check for missing values, you will not get any warning or error messages.
This is an extremely easy error message to make because you are using two functions that have the same arguments, but those functions use them differently. The key to avoiding this problem is to stick with a single way of doing it to avoid confusion. Because of the complexities of these formulas, it is extremely easy to make this mistake. However, once you are aware of it, and know how to fix it, it will be equally easy to avoid. The biggest danger of making this mistake comes if you are using the ggplot function for other types of graphs, and then need to come back to this one. The best long-term solution is to play around with this function until you are so used to it that you will be less likely to make this mistake. Even then, since you now have two ways of fixing it, it will be easy for you to fix and so there is no reason to be concerned about it coming up again.