geom_rect failure:eval(expr,envir,enclos)中的错误:未找到对象“变量” [英] geom_rect failure: Error in eval(expr, envir, enclos) : object 'variable' not found
问题描述
我试图复制ggplot2书籍第86页上的Hadley Wickham的例子,在那里他覆盖了geom_rects,显示了失业趋势的政党。这里是我的核心代码,以及一些示例数据:
library(ggplot2)
library(reshape2 )
library(lubridate)
con <-textConnection(Date,Var1,Var2
8-Jan-12,100.8,116
15-Jan- 12,99.4,115.5
22-Jan-12,98.4,115
28-Jan-12,97.1,114
4-Feb-12,95.9,112
11- 2月-12,95.3,113
18-Feb-12,93.9,111.5
25-Feb-12,93.5,111.5
3-Mar-12,92.6,110
10-Mar-12,91.4,108
17-Mar-12,90.2,106.8
24-Mar-12,90,107.5
31-Mar-12,89.9,106
5-Apr-12,90.4,106.5
12-Apr-12,89.8,106)
track< - read.csv(con,header = TRUE)
跟踪$ Date< - as.Date(dmy(track $ Date))
track< - melt(track,
id.vars = c(Date))
con < - textConnection(City,From,to
奥克兰,5-Apr-12,10-Apr-12
布鲁塞尔,4月1日-12,3月-12
克利夫兰,24-Jan-12,26-Jan-12
大吉岭,18-Jan-12,19-Jan-12
Erehwon,8-Feb-12,10- 2月-12
佛罗伦萨,5-Mar-12,7-Mar-12
甘道夫, 25-Mar-12,27-Mar-12)
旅行< - read.csv(con,header = TRUE)
旅行$ From< - as.Date(dmy (旅行$ From))
旅行$ To< - as.Date(dmy(旅行$ to))
#绘制时间序列数据
p < - ggplot(
aes(x = Date,
y = value,
color = variable,
group = variable))
p < - p + geom_smooth()
p <-p + geom_point(size = 3)
p <-p + geom_vline(data = trips,
aes(xintercept = From),
color ='steelblue')#出发日期
p < - p + geom_vline(data = trips,
aes(xintercept = To),
color ='grey80')#返回日期
yrng < - c (80,120)
p <-p + scale_y_continuous(限制= yrng)
p#这个工程可以使用vlines(丑陋的,但是很重要)
#-----------这个geom_rect()停止绘制这个图形
p2< - p + geom_rect( aes(NULL,NULL,xmin = From,xmax = To),
ymin = yrng [1],ymax = yrng [2],
data = trips)
p2 #this给出错误消息
#eval(expr,envir,enclos)中的错误:object'variable'找不到
#----------- THIS NOW工作,感谢@smu,见下文
p2< - p + geom_rect(aes(NULL,NULL,xmin = From,xmax = To,
color = NULL,group = NULL),
ymin = yrng [1],ymax = yrng [2],
fill ='orange',
alpha = 0.4,
data = trips)
p2#和I已经添加了填充和阿尔法来美化它
正如@ tyler-rinker所建议的,我编辑过该代码包含原始错误和由@smu指出的解决方案。在第一个 ggplot
调用中,您映射 c $ c> color
改为'variable'。该映射仍存在于 geom_rect
调用中,但'trips'中不存在名为'variable'的变量。我建议使用以下方式取消映射:
p2 < - p + geom_rect(aes(NULL,NULL,xmin = From, xmax = To,color = NULL),
ymin = yrng [1],ymax = yrng [2],
data = trips)
(没有尝试过,但我猜它应该可以)。
I am trying to replicate Hadley Wickham's example on page 86 of the ggplot2 book, where he overlays geom_rects showing the party in power over the trend in unemployment. Here is my core code, with some sample data:
library("ggplot2")
library("reshape2")
library("lubridate")
con <- textConnection("Date,Var1,Var2
8-Jan-12,100.8,116
15-Jan-12,99.4,115.5
22-Jan-12,98.4,115
28-Jan-12,97.1,114
4-Feb-12,95.9,112
11-Feb-12,95.3,113
18-Feb-12,93.9,111.5
25-Feb-12,93.5,111.5
3-Mar-12,92.6,110
10-Mar-12,91.4,108
17-Mar-12,90.2,106.8
24-Mar-12,90,107.5
31-Mar-12,89.9,106
5-Apr-12,90.4,106.5
12-Apr-12,89.8,106")
track <- read.csv(con, header=TRUE)
track$Date <- as.Date( dmy(track$Date) )
track <- melt(track,
id.vars = c("Date"))
con <- textConnection("City,From,To
Auckland,5-Apr-12,10-Apr-12
Brussels,1-Apr-12,3-Apr-12
Cleveland,24-Jan-12,26-Jan-12
Darjeeling,18-Jan-12,19-Jan-12
Erehwon,8-Feb-12,10-Feb-12
Florence,5-Mar-12,7-Mar-12
Gandalf,25-Mar-12,27-Mar-12")
trips <- read.csv(con, header=TRUE)
trips$From <- as.Date( dmy(trips$From) )
trips$To <- as.Date( dmy(trips$To) )
# draw the Time Series data
p <- ggplot(track,
aes(x = Date,
y = value,
colour=variable,
group = variable))
p <- p + geom_smooth()
p <- p + geom_point(size=3)
p <- p + geom_vline(data=trips,
aes(xintercept=From),
colour='steelblue') # departure dates
p <- p + geom_vline(data=trips,
aes(xintercept=To),
colour='grey80') # return dates
yrng <- c(80,120)
p <- p + scale_y_continuous(limits=yrng)
p # this works up to here, using vlines (ugly, but makes the point)
# ----------- THIS geom_rect() STOPS THIS PLOT BEING RENDERED
p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To),
ymin=yrng[1], ymax=yrng[2],
data=trips)
p2 # this gives an error message
# Error in eval(expr, envir, enclos) : object 'variable' not found
# ----------- THIS NOW WORKS, THANKS TO @smu, SEE BELOW
p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To,
colour=NULL, group=NULL),
ymin=yrng[1], ymax=yrng[2],
fill='orange',
alpha=0.4,
data=trips)
p2 # and I have added fill and alpha to prettify it a bit
As suggested by @tyler-rinker, I have edited the code to include the original error and the solution pointed out by @smu. Thanks.
In the first ggplot
call you map the colour
to 'variable'. This mapping is still present in the geom_rect
call, but no variable named 'variable' exists in 'trips'. I would suggest to unmap colour using:
p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To, colour=NULL),
ymin=yrng[1], ymax=yrng[2],
data=trips)
(Haven't tried it, but I guess it should work).
这篇关于geom_rect failure:eval(expr,envir,enclos)中的错误:未找到对象“变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!