Lamden
Lamden

Reputation: 25

ggplot2 requiring explicit mention of mapping argument

I have tried this formula which used to work

library(tidyverse)
ggplot()+
  geom_point(iris,aes(Sepal.Length, Sepal.Width))

But I get the following error: Error: mapping must be created by aes()

When I rerun the code with the mapping argument mentioned explicitly, it works:

ggplot()+
  geom_point(iris,mapping=aes(Sepal.Length, Sepal.Width))

I tried reloading RStudio, but still not working... not sure what happened here.

Upvotes: 1

Views: 1119

Answers (2)

TarJae
TarJae

Reputation: 79301

To build a ggplot we use the basic template:

ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) +  <GEOM_FUNCTION>()

This code:

ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point()

is the same with this:

ggplot(data = iris, mapping = aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point()

Upvotes: 1

akrun
akrun

Reputation: 887951

It is just that in geom_point, the first argument is mapping and second is data. As per the Usage in documentation of ?geom_point

geom_point( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )

So, if we don't specify the arguments, it thinks the first argument which is given as iris as mapping

ggplot()+
  geom_point(aes(Sepal.Length, Sepal.Width), iris)

Upvotes: 1

Related Questions