2016-06-16 78 views
1

我使用rasterVis::gplot()繪製由raster程序包創建的柵格圖層。單獨繪製光柵正常工作:在gplot的柵格地圖上繪製矩形

library(raster) 
library(rasterVis) 

r1 <- raster(nrow=10, ncol=10) 
values(r1) <- runif(ncell(r1)) 

gplot(r1) + 
    geom_raster(aes(fill=value)) 

enter image description here

但是當我嘗試對情節USNG geom_rect()添加一個矩形,我得到一個錯誤的eval

df <- data.frame(xmin=-50, xmax=50, ymin=-50, ymax=50) 
gplot(r1) + 
    geom_raster(aes(fill=value)) + 
    geom_rect(data=df, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)) 

錯誤( expr,envir,enclos):未找到對象'y'

我在做什麼錯?

+2

將'inherit.aes = FALSE'添加到geom_rect –

回答

1

geom_rect正期待早些時候宣佈所有美學(明示或暗示),但df中沒有y。使用參數inherit.aes = FALSE關閉此行爲。

gplot(r1) + 
    geom_raster(aes(fill=value)) + 
    geom_rect(data=df, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), 
    inherit.aes = FALSE) 

或者,使用annotate添加矩形。

gplot(r1) + 
    geom_raster(aes(fill=value)) + 
    with(df, annotate(geom = "rect", xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax))