当我的 aes 函数中未提及 XXX 时,geom_text 返回“未找到对象 XXX”错误

San*_*aus 3 r ggplot2

当我运行下面的代码时,出现错误Error in FUN(X[[i]], ...) : object 'typoft' not found。我有两个 geom_texts,因为我在图上放置了更多文本。谁能在这里帮助我并让我知道为什么会出现错误?我需要能够使用第二个 geom_text。

请分享您的想法并提前致谢。

仅供参考 labdat 是我用来绘制某些文本的数据框。

代码:

x = c(-.87,-.87,-.87,-.29,-.29,-.29)
y = c(-.75,-.75,-.75,-.18,-.18,-.18)
label = c(.1,0,.3,.2,.1,1)
trade = c("S","M","L","S","M","L")
labdat <- data.frame(x=x,y=y,label=label,trade = trade)
  

ggplot(gb, aes(x = y, y = y1, fill = typeoft)) + 
    geom_bin2d(bins = 10, aes(alpha = ..count..)) +
    scale_fill_manual(values = c("blue","black")) +
    geom_text(bins = 10, stat = "bin2d", aes(label = round(100*..density..,1)),
              size = 2,check_overlap = TRUE) +
    facet_wrap(~type, nrow = 1) +
    geom_abline(slope=1, intercept=0) +
    scale_alpha_continuous(range = c(.05,1)) +
    theme(panel.background = element_rect(fill = "white")) +
    theme(legend.position = "none") +
    geom_text(data = labdat,aes(x = x,y = y,label = label))
Run Code Online (Sandbox Code Playgroud)

数据(样本):

gb <- structure(list(type = c("M", "M", "S", "S", "M", "S", "S", "S", 
"M", "S", "M", "M", "M", "S", "S", "M", "S", "S", "S", "M", "M", 
"S", "M", "S", "S", "M", "M", "M", "S", "M", "S", "M", "S", "S", 
"S", "M", "S", "M", "M", "S", "S", "S", "S", "M", "S", "M", "M", 
"S", "M", "M", "S", "M", "M", "S", "M", "S", "S", "S", "S", "M", 
"S", "S", "S"), y = c(0.0173, 0.0358, 0.0203, 0.0012, 0.0219, 
0.0137, 0.0345, 0.0267, 0.0135, 0.0254, 0.0179, 0.0313, 0.0268, 
0.0294, 0, 0.0365, 0.0144, 0.0252, 0.0217, 0.0182, 0.0298, 0.0135, 
0.0204, 0.0152, 0.0236, 0.032, 0.0426, 0.0233, 0.0136, 0.011, 
0.0212, 0.0247, 0.0374, 0.0312, 0.0314, 0.0162, 0.0011, 0.0213, 
0, 0.0231, 0.0121, 0.0304, 0.0059, 0.0371, 0.0209, 0.028, 0.0166, 
0.0224, 0.0257, 0.0262, 0.0209, 0.0139, 0.0187, 0.0125, 0.0335, 
0.0301, 0, 0.0176, 0.0269, 0.0184, 0.0204, 0.0165, 0.0158), y1 = c(0.0318, 
0.0227, 0.0412, 0.0012, 0.0187, 0.0166, 0.0317, 0.0272, 0.027, 
0.0259, 0.0144, 0.0276, 0.0267, 0.035, 0.0218, 0.0183, 0.0139, 
0.0223, 0.0227, 0.0275, 0.0259, 0.0317, 0.026, 0.0228, 0.0258, 
0.036, 0.0504, 0.0165, 0.0107, 0.0012, 0.0258, 0.0273, 0.0277, 
0.0283, 0.0327, 0.0331, 0.0224, 0.0248, 0.0245, 0.0213, 0.0315, 
0.03, 0.0269, 0.0389, 0.0302, 0.025, 0.0291, 0.0213, 0.0335, 
0.027, 0.0269, 0.0142, 0.0286, 0.0103, 0.0373, 0.034, 0.0285, 
0.0103, 0.0278, 0.0188, 0.0062, 0.028, 0.0146), typeoft = c("e", 
"c", "e", "c", "c", "e", "c", "e", "e", "e", "c", "c", "c", "e", 
"e", "c", "c", "c", "e", "e", "c", "e", "e", "e", "e", "e", "e", 
"c", "c", "c", "e", "e", "c", "c", "e", "e", "e", "e", "e", "c", 
"e", "c", "e", "e", "e", "c", "e", "c", "e", "e", "e", "e", "e", 
"c", "e", "e", "e", "c", "e", "e", "c", "e", "c")), class = "data.frame", row.names = c(NA, 
-63L))
Run Code Online (Sandbox Code Playgroud)

div*_*san 6

您的问题来自这样一个事实:当您aes在调用中定义 an 时,如果不覆盖这些设置,则这些设置将被其后的ggplot()所有设置继承。geom_*

如果我们将您的问题简化为最小形式,我们就可以清楚地看到这一点。我们可以仅用最后一个来复制您的问题geom_text

ggplot(gb, aes(x = y, y = y1, fill = typeoft)) + 
    geom_text(data = labdat, aes(x = x,y = y,label = label))

Error in FUN(X[[i]], ...) : object 'typeoft' not found
Run Code Online (Sandbox Code Playgroud)

发生这种情况是因为当您定义aesin时,您为、和ggplot设定了值。当您调用 时,和 的值将被覆盖,但 fill 的值不会被覆盖。所以for实际上看起来像这样:。但由于您没有在对象中命名的变量,因此它会返回错误。xyfillaesgeom_textxyaesgeom_text aes(x = x, y = y, label = label, fill = typeoft)typeoftlabdat

geom_text我们可以通过给你论据来阻止这种行为inherit.aes = FALSE

# This works!
ggplot(gb, aes(x = y, y = y1, fill = typeoft)) + 
    geom_text(data = labdat,aes(x = x, y = y, label = label), inherit.aes = FALSE)
Run Code Online (Sandbox Code Playgroud)

现在,aesforgeom_text将只包含您告诉它拥有的内容。