我正在尝试绘制两个变量之间的关系-由点回归线表示。我还想在这个图上添加两个灰色面板,如下图所示。
几天前我写了代码,它运行得很好。但是突然我开始在最后一步收到错误信息"Error in FUN(X[i],...):object 'a‘not found“,而且它还在添加灰色面板。我在谷歌上搜索了这个问题,尝试更新包,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
有人能帮我找到解决方案吗?提前谢谢。
发布于 2020-04-10 00:07:27
在初始ggplot()
中,您需要指定y = a
。此映射将一直转发,直到被覆盖为止。当错误发生时,您指定的数据不包含a
,但a
仍然映射到y
。尝试显式设置y = NULL
以覆盖映射。
发布于 2021-05-07 18:11:44
正如@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
+ ) +
https://stackoverflow.com/questions/61125227
复制相似问题