我無法傳遞存儲在變量中的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)
是的,它完全符合我的意思。我總是喜歡一個簡單的答案,雖然我有點尷尬,這幾乎是一個NOOP ...... :)謝謝! – 2010-09-19 03:15:59