首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FUN(X[[i]],...)出错:找不到对象'a‘

FUN(X[[i]],...)出错:找不到对象'a‘
EN

Stack Overflow用户
提问于 2020-04-09 23:57:28
回答 2查看 2.4K关注 0票数 0

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

几天前我写了代码,它运行得很好。但是突然我开始在最后一步收到错误信息"Error in FUN(X[i],...):object 'a‘not found“,而且它还在添加灰色面板。我在谷歌上搜索了这个问题,尝试更新包,R并重新启动它。所有这些似乎都不起作用。下面是代码和输出,供您参考。

代码语言:javascript
运行
复制
> # 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

有人能帮我找到解决方案吗?提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-10 00:07:27

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

票数 2
EN

Stack Overflow用户

发布于 2021-05-07 18:11:44

正如@mnist解释的那样,美学继承自ggplot()中的初始绘图规范。

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

代码语言:javascript
运行
复制
> tpr <- tpr +
+   geom_ribbon(
+     data = tpreg3,
+     aes(x = t, ymin = func1, ymax = func2),
+     fill = "grey",
+     alpha = .25,
+     inherit.aes = FALSE
+   ) + 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61125227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档