2010-09-17 59 views
7

我無法傳遞存儲在變量中的POSIXct作爲geom_rect的xmin/xmax。我試圖構建一個獨立的示例,而不會使我想要做的事情變得無足輕重......如何從變量傳遞ggplot2審美?

這個想法是採用一個ggplot2繪圖對象,其中x是一個POSIXt,並且「放大」特定的時間範圍。變焦位於前80%,整個系列位於最後20%,指示器上部放大了部分。

我的問題是,我似乎無法得到傳入geom_rect的xmin/xmax - 我嘗試過的每件事(除了用手組裝圖而不是函數)給了我一個不同的錯誤。我已經嘗試使用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) 

回答

8

Hmmmm,我想你需要一個新的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) 

給你想要的東西。

+0

是的,它完全符合我的意思。我總是喜歡一個簡單的答案,雖然我有點尷尬,這幾乎是一個NOOP ...... :)謝謝! – 2010-09-19 03:15:59

0

你只需要改變你的函數參數限制的名稱,因爲我認爲這是創建一個作用域衝突。我只是將其更改爲limits1,並且還將代碼中的第一行讀取lims = as.POSIXct(limits1),並且它完美地工作。一探究竟!!

+0

你可以發佈你的工作代碼?我做出了你所建議的改變,根本沒有看到任何改變。存儲在lims [1:2]中的值在從函數內部打印時看起來是合理的,這正是我想要傳遞給geom_rect的。不過,我欣賞這個建議! – 2010-09-17 19:01:42

1

重寫:哈德利的回答

由於的ggplot2較新版本的更新,通過哈德利, 更直觀的方式處理的功能ggplot() NON-STD評價使用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) 
+1

看來'aes_'也會提供答案。 [幫助](http://docs.ggplot2.org/current/aes_.html)提到「'aes_q'是'aes_'」的別名。 – 2016-12-01 09:51:37