geom_ribbon不起作用 - eval中的错误(expr,envir,enclos):找不到对象'变量' [英] geom_ribbon doesn't work - Error in eval(expr, envir, enclos) : object 'variable' not found
问题描述
我尝试添加一个 geom_ribbon
对象到我的 ggplot2
图中。在我的数据框中,我有 NA
值(我猜)可能会导致问题。这是一个我可以重复使用的数据框的例子:
$ p $ base < - c(1:10,rep(NA, 10))
output1 <-c(rep(NA,9),10-0:10)
output2 <-c(rep(NA,9),10 + 0:10)
xaxis < - 1:20
df< - data.frame(xaxis,base,output1,output2)
df
xaxis base output1输出2
1 1 1不适用
2 2 2不适用
3 3 3不适用
4 4 4不适用
5 5 5不适用
6 6 6不适用
7 7 7不适用
8 8 8不适用不适用
9 9 9不适用不适用
10 10 10 10 10
11 11不适用9 11
12 12 NA 8 12
13 13 NA 7 13
14 14 NA 6 14
15 15 NA 5 15
16 16 NA 4 16
17 17不适用3 17
18 18不适用2 18
19 19 NA 1 19
20 20 NA 0 20
我的尝试用 geom_ribbon
绘制 ggplot2
对象:
<$ (df,id = 1)
ggplot(dfm,aes(x = xaxis,y = value,color = variable))+
geom_line(aes(group = variable))+
geom_ribbon(data = df,aes(group = 1,ymin = output1,ymax = output2))
最终,我得到了一个我无法处理的错误:
<$ p $ eval(expr,envir,enclos)中的错误:object'variable'not found
预先感谢您的任何建议。
你得到这个错误是因为变量
code> aes()函数 ggplot()
。当您使用新数据框添加 geom_ribbon()
geom_ribbon()
试图找到变量
在新数据框中用于颜色。要忽略这个变量,请在 geom_ribbon()
中添加 inherit.aes = FALSE
- 所以您要告诉所有参数应该是独立采取 - 这就是你应该在 geom_ribbon()
中再次设置 x = xaxis
。
ggplot(dfm,aes(x = xaxis,y = value,color = variable))+
geom_line(aes(group = variable)) +
geom_ribbon(data = df,aes(group = 1,x = xaxis,ymin = output1,ymax = output2),
inherit.aes = FALSE)
I try to add a geom_ribbon
object to my ggplot2
plot. In my data frame, I have NA
values that (I guess) may cause a problem. This is a reproducible example of the data frame I have:
base <- c(1:10, rep(NA, 10))
output1 <- c(rep(NA, 9), 10 - 0:10)
output2 <- c(rep(NA, 9), 10 + 0:10)
xaxis <- 1:20
df <- data.frame(xaxis, base, output1, output2)
df
xaxis base output1 output2
1 1 1 NA NA
2 2 2 NA NA
3 3 3 NA NA
4 4 4 NA NA
5 5 5 NA NA
6 6 6 NA NA
7 7 7 NA NA
8 8 8 NA NA
9 9 9 NA NA
10 10 10 10 10
11 11 NA 9 11
12 12 NA 8 12
13 13 NA 7 13
14 14 NA 6 14
15 15 NA 5 15
16 16 NA 4 16
17 17 NA 3 17
18 18 NA 2 18
19 19 NA 1 19
20 20 NA 0 20
And my attempt to plot a ggplot2
object with a geom_ribbon
:
dfm <- melt(df, id=1)
ggplot(dfm, aes(x = xaxis, y = value, colour = variable)) +
geom_line(aes(group=variable)) +
geom_ribbon(data=df, aes(group = 1, ymin=output1, ymax=output2))
And, eventually, I got an error I cannot deal with:
Error in eval(expr, envir, enclos) : object 'variable' not found
Thank you in advance for any suggestions.
You got this error because variable
is used for color in aes()
of function ggplot()
. When you add geom_ribbon()
with new data frame geom_ribbon()
tries to find variable
in new data frame to use it for colors. To ignore this variable add inherit.aes=FALSE
inside geom_ribbon()
- so you are telling that all parameters should be taken independently - that's way you should set x=xaxis
again in geom_ribbon()
.
ggplot(dfm, aes(x = xaxis, y = value, colour = variable)) +
geom_line(aes(group=variable)) +
geom_ribbon(data=df, aes(group = 1, x = xaxis,ymin=output1, ymax=output2),
inherit.aes=FALSE)
这篇关于geom_ribbon不起作用 - eval中的错误(expr,envir,enclos):找不到对象'变量'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!