散点图:在FUN(X[[i]], ...)中出现错误:未找到对象“Group”

19

我正在尝试使用ggplot绘制一些数据,但在显著性线和星号方面遇到了一些问题。

这是我所使用的代码:

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)

这导致我出现以下错误:

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

我知道问题出在 geom_path(data = path, aes(x = x,y = y)) 这里,但我有点迷失方向。我是 ggplot 的新手,希望能得到一些简单的帮助。

有什么建议吗?

1个回答

46

aesthetics默认是继承的。 geom_path尝试查找path数据集中的Group变量以获取颜色。您应该在geom_path上使用inherit.aes = FALSE

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

3
对于任何已经尝试过 inherit.aes = FALSE 的人,不要忘记同时添加 aes(x = x, y = y)。这就是我所缺少的。 - stevec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接