如何从变量传递ggplot2美学?

Ste*_*mer 7 r 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
Run Code Online (Sandbox Code Playgroud)

我认为我的问题是,当美学得到处理时,我用来设置美学的变量不在范围内,但我无法弄清楚如何去做.救命.

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)
Run Code Online (Sandbox Code Playgroud)

had*_*ley 8

嗯,我认为你需要一个aes有点像的新函数aes(因为它不会尝试解析它的参数)并且有点像aes_string(因为它在本地环境中立即评估它的参数):

aes_now <- function(...) {
  structure(list(...),  class = "uneval")
}
Run Code Online (Sandbox Code Playgroud)

然后

bottom <- bottom + geom_rect(aes_now(xmin=lims[1], xmax=lims[2]),
 ymin=-Inf, ymax=Inf, fill="grey80", alpha=0.01)
Run Code Online (Sandbox Code Playgroud)

给你你想要的.