Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R FUN(X[[i]],…)中的错误:对象';a';找不到_R_Ggplot2 - Fatal编程技术网

R FUN(X[[i]],…)中的错误:对象';a';找不到

R FUN(X[[i]],…)中的错误:对象';a';找不到,r,ggplot2,R,Ggplot2,我试图绘制两个变量之间的关系——用点回归线表示。我还想在此图上添加两个灰色面板,如下图所示 几天前我写了代码,它工作得很好。但突然间,我开始在最后一步收到错误消息“error in FUN(X[[I]],…):object'a'not found”,并且它正在添加灰色面板。我在谷歌上搜索这个问题,尝试更新软件包,然后重新启动它。这些似乎都不管用。 以下是代码和输出,供您参考 > # read files > > tpreg1=read.csv("tpreg1.csv", he

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

几天前我写了代码,它工作得很好。但突然间,我开始在最后一步收到错误消息“error in FUN(X[[I]],…):object'a'not found”,并且它正在添加灰色面板。我在谷歌上搜索这个问题,尝试更新软件包,然后重新启动它。这些似乎都不管用。 以下是代码和输出,供您参考

> # 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
#读取文件
> 
>tpreg1=read.csv(“tpreg1.csv”,header=TRUE)
>tpreg2=read.csv(“tpreg2.csv”,header=TRUE)
>tpreg3=read.csv(“tpreg3.csv”,header=TRUE)
> 
>tpreg1
塔塔
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
塔塔
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
> 
>#图书馆
>图书馆(GG2)
> 
> 
> 
>#CS点、回归线、置信区间
>tpr
>#刻度
>tpr
>#纵横比
>tpr
> 
>#面板设置
>tpr
>#轴线和记号
>tpr
>tpr
`geom_smooth()`使用公式“y~x”
> 
>#面板
> 
>#面板上下限的直线方程
> 
>#下部暗色面板,ymin
>func1=sapply(
+tpreg3$t,
+乐趣=功能(x){
+-1*x+16
+   }
+ )
>职能1
[1]  -5.5  -6.0 -10.0 -12.0 -16.0 -16.5
>#下部暗色面板,ymax
>func2=sapply(
+tpreg3$t,
+乐趣=功能(x){
+0*x+0
+   }
+ )
>功能2
[1] 0 0 0 0 0 0
> 
>#上部深色面板,ymin
>func3=sapply(
+tpreg3$t,
+乐趣=功能(x){
+0*x+16
+   }
+ )
>职能3
[1] 16 16 16 16 16 16
>#上部深色面板,ymax
>func4=sapply(
+tpreg3$t,
+乐趣=功能(x){
+1*x+0
+   }
+ )
>职能4
[1] 21.5 22.0 26.0 28.0 32.0 32.5
> 
> 
>#灰色填充
>tpr
>tpr
FUN(X[[i]],…)中出错:未找到对象“a”

有人能帮我找到解决办法吗?提前感谢。

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

正如@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
+   ) + 

>tpr非常感谢@mnist,它成功了!我试图了解更多关于这类错误的信息,但无法轻松阅读。你能推荐任何能为这种错误提供简单解释的链接吗。