【问题标题】:Error in FUN(X[[i]], ...) : object 'a' not foundFUN(X[[i]], ...) 中的错误:找不到对象“a”
【发布时间】:2023-12-20 20:10:01
【问题描述】:

我正在尝试绘制两个变量之间的关系 - 由点回归线表示。我还想在该图上添加两个灰色面板,如下图所示。

我几天前编写了代码,它运行良好。但是突然间我开始在最后一步收到错误消息“FUN(X [[i]],...)中的错误:找不到对象'a'”,并且它正在添加灰色面板。我用谷歌搜索了这个问题,尝试更新包、R 并重新启动它。这些似乎都不起作用。 这是代码和输出供您参考。

> # read files
> 
> tpreg1=read.csv("tpreg1.csv", header = TRUE)
> tpreg2=read.csv("tpreg2.csv", header = TRUE)
> tpreg3=read.csv("tpreg3.csv", header = TRUE)
> 
> tpreg1
   t     a
1 22 15.29
2 24 14.87
3 26 14.43
4 28 13.19
5 30 12.69
6 32 10.58
> tpreg2
   t     a
1 22 13.13
2 24 11.47
3 26 10.01
4 28  8.02
5 30  5.81
6 32  3.25
> tpreg3
     t
1 21.5
2 22.0
3 26.0
4 28.0
5 32.0
6 32.5
> 
> # Library 
> library(ggplot2)
> 
> 
> 
> # CS points, regression line, confidence intervals
> tpr <-
+   ggplot(tpreg1, aes(x = t, y = a)) + geom_point(
+     shape = 1,
+     fill = "darkgreen",
+     alpha = 1,
+     color = "darkgreen",
+     size = 1
+   ) + geom_smooth(
+     method = lm,
+     size = 0.5,
+     linetype = "solid",
+     color = "darkgreen"
+   )
> 
> # Scale
> tpr <- tpr +
+   expand_limits(x = c(21.5, 32.5), y = c(-16, 34)) +
+   scale_x_continuous(expand = c(0, 0),
+                      breaks = c(22, 24, 26, 28, 30, 32)) +
+   scale_y_continuous(expand = c(0, 0),
+                      breaks = c(-16, -8,  0, 8,  16, 24, 32))
> 
> # Aspect ratio
> tpr<- tpr + theme(aspect.ratio = 0.75)
> 
> 
> # Panel settings
> tpr <- tpr +
+   theme(panel.background = element_blank()) +
+   theme(panel.grid.minor = element_blank(),
+         panel.grid.major = element_blank())
> 
> # Axes lines and ticks 
> tpr <- tpr +
+   theme(
+     axis.line.x = element_line(colour = "black", size = 0.5),
+     axis.line.y = element_line(colour = "black", size = 0.5),
+     axis.ticks.y = element_line(colour = "black", size = 0.5),
+     axis.ticks.x = element_line(colour = "black", size = 0.5),
+     axis.ticks.length = unit(.1, "cm")
+   ) 
> 
> tpr
`geom_smooth()` using formula 'y ~ x'
> 
> # Panels
> 
> # straight line equations upper and lower limits of the panels
> 
> # lower dark panel, ymin
> func1 = sapply(
+   tpreg3$t,
+   FUN = function(x) {
+     -1 * x + 16
+   }
+ )
> func1
[1]  -5.5  -6.0 -10.0 -12.0 -16.0 -16.5
> # lower dark panel, ymax
> func2 = sapply(
+   tpreg3$t,
+   FUN = function(x) {
+     0 * x + 0
+   }
+ )
> func2
[1] 0 0 0 0 0 0
> 
> # upper dark panel, ymin
> func3 = sapply(
+   tpreg3$t,
+   FUN = function(x) {
+     0 * x + 16
+   }
+ )
> func3
[1] 16 16 16 16 16 16
> # upper dark panel, ymax
> func4 = sapply(
+   tpreg3$t,
+   FUN = function(x) {
+     1 * x + 0
+   }
+ )
> func4
[1] 21.5 22.0 26.0 28.0 32.0 32.5
> 
> 
> # Grey filling
> tpr <- tpr +
+   geom_ribbon(
+     data = tpreg3,
+     aes(x = t, ymin = func1, ymax = func2),
+     fill = "grey",
+     alpha = .25
+   ) + geom_ribbon(
+     data = tpreg3,
+     aes(x = t, ymin = func3, ymax = func4),
+     fill = "grey",
+     alpha = .25
+   )
> 
> tpr
Error in FUN(X[[i]], ...) : object 'a' not found

谁能帮我找到解决办法?提前致谢。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    正如@mnist 所解释的,美学继承自ggplot() 中的初始情节规范。

    一个更简洁的解决方案是在geom_ribbon() 的后续层中将默认的inherit.aes = TRUE 更改为FALSE,即:

    > tpr <- tpr +
    +   geom_ribbon(
    +     data = tpreg3,
    +     aes(x = t, ymin = func1, ymax = func2),
    +     fill = "grey",
    +     alpha = .25,
    +     inherit.aes = FALSE
    +   ) + 
    

    【讨论】:

      【解决方案2】:

      在您的初始ggplot() 中,您指定y = a。此映射被转发,直到它被覆盖。发生错误时,您指定的数据不包含a,但a 仍映射到y。尝试显式设置y = NULL 以覆盖映射。

      【讨论】:

      • 非常感谢@mnist,它成功了!我试图更多地了解这种错误,但无法轻松阅读。您能否建议任何可以为此类错误提供简单解释的链接。