Error: geom_point requires the following missing aesthetics: x, y

Hi,
I would like to jitter some points but run into error saying x,y missing from aes. However, they are clearly there! Below code runs without + scale_x_continuous() + geom_jitter()
Thanks

library(magrittr)
library(tidyverse)
out<-tribble(
  ~id, ~value,
  1,   15,
  2,   20,
  3,   30
)
ggplot(out) +
  geom_point(aes(y=value,x=id)) + 
  scale_x_continuous() + 
  geom_jitter()

geom_jitter also requires the missing aesthetics, so you can:

ggplot(out)+geom_point(aes(y=value,x=id)) + scale_x_continuous() + geom_jitter(aes(y=value,x=id))

or

ggplot(out,aes(y=value,x=id))+geom_point() + scale_x_continuous() + geom_jitter()
1 Like

Yep, just as @rafaelmenmell showed, you can make things easier on yourself by specifying the aesthetics in the original ggplot() call, if you're using the same aesthetics for x and y for both geoms.

library(tidyverse)

out <- tribble(
  ~id, ~value,
  1, 15,
  2, 20,
  3, 30
)
ggplot(out, aes(y = value, x = id)) + 
  geom_point() + 
  scale_x_continuous() + 
  geom_jitter()

Created on 2019-03-15 by the reprex package (v0.2.1.9000)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.