Scatterplot: Error in FUN(X[[i]], ...) : object 'Group' not found

Tato14 picture Tato14 · Jul 8, 2017 · Viewed 37k times · Source

I'm trying to plot some data using ggplot and I'm having some problems with the significant lines and asterisk.

This is the code I am using:

p <- ggplot(Hematoxilin_tumor_necrosis, aes(x=total, y=necro, colour = Group))+
  labs(y="Necrotic area",x="Total area")+
  theme_minimal()

path = data.frame(x=c(78,79,79,78),y=c(22,22,34,34))

p + geom_point(size=0.7)+
  geom_smooth(method=lm, se = F, size=0.8) +
  scale_color_manual(values=c("#999999","#333333"))+
  #Adding asterisks
  geom_path(data = path, aes(x = x,y = y)) +
  annotate("text",x = 80, y = 27, label="*", cex=7)

Which gives me the following error:

Error in FUN(X[[i]], ...) : object 'Group' not found

I know that the problem is in the geom_path(data = path, aes(x = x,y = y)) but I am kind of lost. I am new in ggplot so I expect some simple problem.

Any advice?

Answer

Marcelo picture Marcelo · Jul 8, 2017

aesthetics are inherited by default. The geom_path is trying to look for the Group variable on the path dataset to get the color. You should use inherit.aes = FALSE on the geom_path:

  geom_path(data = path, aes(x = x,y = y), inherit.aes = FALSE )