当添加具有不同数据的图层时,ggplot对象未找到错误 [英] ggplot object not found error when adding layer with different data

查看:133
本文介绍了当添加具有不同数据的图层时,ggplot对象未找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些积分,我想用segment来连接它们。

  dummy = data.frame (GROUP = c(A,B,C,D),
X = c(80,75,68,78),
Y = c(30,32 (80),x2 = c(78),y1 = c(30),y2 = c(33),b3 b)b

df = data.frame ))
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)

但我得到这个错误$ / $>

$ pre $ eval(expr,envir,enclos)中的错误:object'GROUP'not发现

我在这里做错了什么?

ggplot 调用中定义的美学映射将被所有图层继承。由于您使用 color = GROUP 初始化了您的绘图,因此 ggplot 会查找 GROUP code>>列,如果它不存在则抛出一个错误。有三个很好的选择可以解决这个问题:



选项1:设置 inherit.aes = F 在你不想继承美学的层中。大多数时候这是最好的选择。

  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)

选项2:只指定您想要的美学在顶层调用中被继承(或者你将覆盖) - 在层级设置其他美学:

  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)

选项3: NULL 图层上的美学效果不适用。

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






使用哪一个?



大部分时间选项1都很好。然而,如果你想让一层美学继承一层,而你只想修改一个或两个,可能会很烦人。也许你在图中添加了一些错误条,并在主数据中使用了相同的 x color 列名,错误栏数据,但是您的错误栏数据没有 y 列。这是使用选项2或选项3以避免重复 x 颜色映射的好时机。 p>

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?

解决方案

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 = F in the layer the you do not want to inherit aesthetics. Most of the time this is the best choice.

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: Specifically 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.)

这篇关于当添加具有不同数据的图层时,ggplot对象未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆