tags:

views:

100

answers:

2

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)
A: 

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!!

Ramnath
Can you post your working code? I made the changes you suggested and did not see any difference at all. The values stored in lims[1:2] seem reasonable when printed from inside the function, and that is what I'm trying to pass to geom_rect. I do appreciate the suggestion, though!
Stephen Ulmer
+1  A: 

Hmmmm, I think you need a new aes function that is a bit like aes (in that it doesn't try to parse its arguments) and a bit like aes_string (in that it evaluates its arguments immediately in the local environment):

aes_now <- function(...) {
  structure(list(...),  class = "uneval")
}

Then

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

gives you what you want.

hadley
Yes, it does exactly what I meant. I always love a simple answer, though I am a little embarrassed that's it almost a NOOP... :) Thank you!
Stephen Ulmer