Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error: Mapping should be created with `aes()` or `aes_()`."

Tags:

I'm trying to build a simple histogram with ggplot2 package in R. I'm loading my data from a csv file and putting 2 of the columns together in a data frame, like this:

df = data.frame(sp = data$species, cov = data$totalcover) 

sp is recognised as a factor of 23 levels (my number of lines) and cov as 23 numbers. Then, to build the histogram, I'm executing this:

ggplot(df, aes(df$sp, df$cov) + geom_histogram()) 

However, R returns the error: "Error: Mapping should be created with aes() or aes_()."

How is this possible if I'm already using aes? Is it maybe related with the type of the values?

like image 343
Sonia Olaechea Lázaro Avatar asked Jul 08 '18 18:07

Sonia Olaechea Lázaro


People also ask

What does error mapping must be created by AES ()`?

How to Fix in R: error: `mapping` must be created by `aes()` This error occurs when you attempt to use the aes() argument when creating a plot in ggplot2 and use it in the incorrect place or use it without the 'mapping' syntax. The following example shows how to fix this error in practice.

What is AES mapping?

aes.Rd. Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms. Aesthetic mappings can be set in ggplot() and in individual layers.


2 Answers

I had the same error, even if I was using aes(). So I used "mapping" before aes()

ggplot()+ geom_boxplot (df, mapping = aes(x= sp, y= cov)) 
like image 200
sebak Avatar answered Sep 19 '22 18:09

sebak


Two mistakes:

  1. Brackets have to be closed after ggplot and calling histogram comes after
  2. you specify your data set when you call ggplot on df. Therefore there is no need to add df$sp. sp is enough.

This code should work (if there is nothing wrong with your data):

    ggplot(df, aes(sp, cov)) + geom_histogram() 
like image 30
LN_P Avatar answered Sep 23 '22 18:09

LN_P