31

I have a plot with some points and I'd like to use segment to connect them

dummy = data.frame(GROUP=c("A","B","C","D"),
                   X = c(80,75,68,78),
                   Y=c(30, 32,36,33)

)
df= data.frame(x1 = c(80), x2 =c(78) , y1=c(30), y2 =c(33))
df
library(ggplot2)
ggplot(dummy,aes(x=X,y=Y,color=GROUP)) + 
  geom_point() +
  geom_segment(aes(x=x1,y=y1,xend= x2, yend =y2), data = df) 

but I get this error

Error in eval(expr, envir, enclos) : object 'GROUP' not found

What am I doing wrong here?

1
  • The best I could find for a dupe... but I like the simplicity of this one. And the answer at the dupe is perhaps a little dated. Commented Nov 2, 2016 at 23:13

1 Answer 1

48

Aesthetic mapping defined in the initial ggplot call will be inherited by all layers. Since you initialized your plot with color = GROUP, ggplot will look for a GROUP column in subsequent layers and throw an error if it's not present. There are 3 good options to straighten this out:

Option 1: Set inherit.aes = FALSE in the layer the you do not want to inherit aesthetics. This is the best choice when aesthetic exceptions are in a small number of layers.

ggplot(dummy,aes(x = X, y = Y, color = GROUP)) + 
  geom_point() +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
               data = df,
               inherit.aes = FALSE) 

Option 2: Only specify aesthetics that you want to be inherited (or that you will overwrite) in the top call - set other aesthetics at the layer level:

ggplot(dummy,aes(x = X, y = Y)) + 
  geom_point(aes(color = GROUP)) +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
               data = df) 

Option 3: Specify NULL aesthetics on layers when they don't apply.

ggplot(dummy,aes(x = X, y = Y, color = GROUP)) + 
  geom_point() +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, color = NULL),
               data = df) 

Which to use?

Most of the time option 1 is just fine. It can be annoying, however, if you want some aesthetics to be inherited by a layer and you only want to modify one or two. Maybe you are adding some errorbars to a plot and using the same x and color column names in your main data and your errorbar data, but your errorbar data doesn't have a y column. This is a good time to use Option 2 or Option 3 to avoid repeating the x and color mappings.)

Sign up to request clarification or add additional context in comments.

3 Comments

for a consistent legend, option 3 could have show.legend = FALSE added
I get the error when + annotation_custom(..), but when + annotation_custom(..., inherit.aes = F) I then see: unused argument (inherit.aes = F)
On further investigation, I may have stumbled upon an edgecase (see here, here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.