Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 439
Tato14 Avatar asked Jul 08 '17 17:07

Tato14


1 Answers

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 )
like image 65
Marcelo Avatar answered Nov 02 '22 22:11

Marcelo