如何从变量传递 ggplot2 美学?
我在传递存储在变量中的 POSIXct 作为 geom_rect 的 xmin/xmax 时遇到问题。我试图构建一个独立的示例,而不是轻视我正在尝试做的事情...
这个想法是采用一个 ggplot2 绘图对象,其 x 是 POSIXt,并在特定时间范围内“放大” 。缩放位于顶部 80%,整个系列位于底部 20%,并在顶部显示哪个部分被缩放的指示器。
我的问题是,我似乎无法将 xmin/xmax 传递到 geom_rect 中——我尝试过的每件事(除了手动而不是函数组装绘图)都会给我一个不同的错误。我尝试过使用 aes()、aes_string()、作为参数而不是美观传递、仅传递字符串等。
下面的示例告诉我:
Error in eval(expr, envir, enclos) : object 'lims' not found
我认为我的问题是我用来设置美观的变量是“当美学得到处理时,它就在范围内,但我不知道还能怎么做。帮助。
library(ggplot2)
subplot <- function(x, y) viewport(layout.pos.col=x, layout.pos.row=y)
vplayout <- function(x, y) {
grid.newpage()
pushViewport(viewport(layout=grid.layout(y,x)))
}
anm_zoom <- function(limits, p) {
lims <- as.POSIXct(limits)
limlab <- paste(lims, collapse=" to ")
top <- p + scale_x_datetime(limlab, limits=lims, expand=c(0,0))
bottom <- p;
bottom <- bottom + opts(title="")
bottom <- bottom + opts(legend.position="none")
bottom <- bottom + opts(axis.title.y=theme_blank())
bottom <- bottom + scale_x_datetime("", expand=c(0,0))
bottom <- bottom + geom_rect(aes(xmin=lims[1], xmax=lims[2]),
ymin=-Inf, ymax=Inf, fill="grey80", alpha=0.01)
## Render the plots
vplayout(1,5)
print(top, vp=subplot(1,c(1,2,3,4)))
print(bottom, vp=subplot(1,5))
}
pdate <- seq.POSIXt(from=as.POSIXct("2010-09-09 00:00"),
to=as.POSIXct("2010-09-10 23:59"), by="2 mins")
var1 <- rnorm(length(pdate))
var2 <- rnorm(length(pdate))
df1 <- data.frame(pdate, var1, var2)
dm <- melt(df1, id="pdate")
p <- ggplot(dm) + aes(x=pdate, y=value) + stat_summary(fun.y="sum", geom="line")
anm_zoom(c("2010-09-09 12:15", "2010-09-09 12:30"), p)
I'm having trouble passing a POSIXct stored in a variable as the xmin/xmax for geom_rect. I've tried to construct a free-standing example without trivializing what I'm trying to do...
The idea is to take a ggplot2 plot object, whose x is a POSIXt, and "zoom in" on a particular range in time. The zoom is in the top 80% and the entire series is in the bottom 20% with an indicator as to what part is zoom'd on top.
My problem is that I can't seem to get the xmin/xmax passed into geom_rect -- each thing I've tried (other than assembling the plot by hand instead of function) gives me a different error. I've tries using aes(), aes_string(), passing as parameters instead of aesthetics, passing just strings, etc.
The example below tells me:
Error in eval(expr, envir, enclos) : object 'lims' not found
I think my problem is that the variables I'm using to set the aesthetics aren't in scope when the aesthetics get processed, but I can't figure out how else to go about it. Help.
library(ggplot2)
subplot <- function(x, y) viewport(layout.pos.col=x, layout.pos.row=y)
vplayout <- function(x, y) {
grid.newpage()
pushViewport(viewport(layout=grid.layout(y,x)))
}
anm_zoom <- function(limits, p) {
lims <- as.POSIXct(limits)
limlab <- paste(lims, collapse=" to ")
top <- p + scale_x_datetime(limlab, limits=lims, expand=c(0,0))
bottom <- p;
bottom <- bottom + opts(title="")
bottom <- bottom + opts(legend.position="none")
bottom <- bottom + opts(axis.title.y=theme_blank())
bottom <- bottom + scale_x_datetime("", expand=c(0,0))
bottom <- bottom + geom_rect(aes(xmin=lims[1], xmax=lims[2]),
ymin=-Inf, ymax=Inf, fill="grey80", alpha=0.01)
## Render the plots
vplayout(1,5)
print(top, vp=subplot(1,c(1,2,3,4)))
print(bottom, vp=subplot(1,5))
}
pdate <- seq.POSIXt(from=as.POSIXct("2010-09-09 00:00"),
to=as.POSIXct("2010-09-10 23:59"), by="2 mins")
var1 <- rnorm(length(pdate))
var2 <- rnorm(length(pdate))
df1 <- data.frame(pdate, var1, var2)
dm <- melt(df1, id="pdate")
p <- ggplot(dm) + aes(x=pdate, y=value) + stat_summary(fun.y="sum", geom="line")
anm_zoom(c("2010-09-09 12:15", "2010-09-09 12:30"), p)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我认为您需要一个新的
aes
函数,它有点像aes
(因为它不会尝试解析其参数)并且有点像aes_string
(因为它立即在本地环境中评估其参数):然后
给你你想要的东西。
Hmmmm, I think you need a new
aes
function that is a bit likeaes
(in that it doesn't try to parse its arguments) and a bit likeaes_string
(in that it evaluates its arguments immediately in the local environment):Then
gives you what you want.
重写:来自hadley的回答
由于hadley更新了新版本
ggplot2
,处理函数中 ggplot() 的非标准评估的更直观方法是使用 aes_q() ,如下所示:
Rewrite: from hadley's answer
Owing to the updates of newer version
ggplot2
by hadley,a more intuitive way dealing with non-std evaluation of
ggplot()
in function is usingaes_q()
as following:您只需要更改函数参数限制的名称,因为我认为这会造成范围冲突。我刚刚将其更改为 limit1 以及代码中的第一行以读取 lims = as.POSIXct(limits1) 并且它运行良好。一探究竟!!
you just need to change the name of your function argument limits as i think it is creating a scoping conflict. i just changed it to limits1 and also the first line in your code to read lims = as.POSIXct(limits1) and it works perfectly. check it out!!