如何从变量传递 ggplot2 美学?

发布于 2024-09-24 01:59:03 字数 1736 浏览 1 评论 0原文

我在传递存储在变量中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛装女皇 2024-10-01 01:59:03

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

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

然后

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

给你你想要的东西。

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.

鸵鸟症 2024-10-01 01:59:03

重写:来自hadley的回答

由于hadley更新了新版本ggplot2
处理函数中 ggplot() 的非标准评估的更直观方法是使用 aes_q() ,如下所示:

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

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 using aes_q() as following:

  xminName  <- substitute(lims[1]); xmaxName  <- substitute(lims[2])
  bottom <- bottom + 
    geom_rect(aes_q(xmin=xminName, xmax=xmaxName),
              ymin=-Inf, ymax=Inf, fill="grey80", alpha=0.01)
短叹 2024-10-01 01:59:03

您只需要更改函数参数限制的名称,因为我认为这会造成范围冲突。我刚刚将其更改为 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文