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?
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.
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.
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))
Two mistakes:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With