Could anybody help me! Cannot add ggproto objects together


#1

Could anybody help me to check the code below and let me know why this "Cannot add ggproto objects together" keeps popping up..

ggplot(data = npa_meta_paper_form, aes(X= advantage, Y = optout)) 
> geom_point(size = 1, colour = "black") + geom_point(size = 1, colour = "white") + geom_smooth(aes(colour = "black"),method = 'lm') + ggtitle("chosen adventage vs optout rate") 
+theme(legend.position = "none")

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?


#2

Hi! Welcome!

Looks like just a simple typo :slightly_smiling_face:

Removing the arguments to make the structure of the code a little clearer, you've got:

ggplot(...) 
geom_point(...) + geom_point(...) + geom_smooth(...) + ggtitle(...) + theme(...)

instead of:

ggplot(...) +  # <-- note this plus sign!
geom_point(...) + geom_point(...) + geom_smooth(...) + ggtitle(...) + theme(...)

#3

Thanks so much! I've added the +, and then it has this error. Sorry it seems to be a very low-level question, is there any resources that I can look it also?

ggplot(data = npa_meta_paper_form, aes(X= advantage, Y = optout)) + geom_point(size = 1, colour = "black") + geom_point(size = 1, colour = "white") + geom_smooth(aes(colour = "black"),method = 'lm') + ggtitle("chosen adventage vs optout rate") +theme(legend.position = "none")
Error: stat_smooth requires the following missing aesthetics: x, y


#4

my data looks like this, but it has more entries (n=82)

image


#5

If you're running the code exactly as you pasted it in here, then I think you've stumbled on the fact that R is case-sensitive.

In your ggplot() call, you define the X and Y aesthetics, when you need to be defining the x and y aesthetics. So when geom_smooth() goes to look for the required x and y aesthetics, it's only finding X and Y, which are not the same thing.

Two good starting points for using ggplot2:

(by the way, you can format your code properly by selecting it and clicking the little </> button at the top of the posting box)