Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a ggplot2 aesthetic from a variable?

Tags:

r

ggplot2

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)
like image 713
Stephen Ulmer Avatar asked Sep 17 '10 17:09

Stephen Ulmer


Video Answer


1 Answers

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.

like image 83
hadley Avatar answered Oct 11 '22 22:10

hadley