2017-02-09 23 views
1

Есть ли способ добавить другой полигон к графику растрового стека, построенного с использованием levelplot? Например, я построил растровый стек, состоящий из 6 растров. Я хотел бы добавить к каждой панели другой полигон. Однако, когда я добавляю слой, этот слой повторяется на всех шести панелях &, мои попытки добавления добавочных полигонов приводят к тому, что они добавляются к каждой панели.Добавление различных полигонов к графику растрового стека с использованием levelplot в R

#plot raster stack 
levelplot(rStack, margin=F, xlab="", ylab="", 
par.settings=list(strip.background=list(col="lightgray")), 
names.attr=c("18000BP", "15000BP", "12000BP", "6000BP", "2050", "2090"), 
     scales = list(draw=F)) 

levelplot of raster stack without polygons

#plot raster stack & add polygons 
levelplot(rStack, margin=F, xlab="", ylab="", 
par.settings=list(strip.background=list(col="lightgray")), 
names.attr=c("18000BP", "15000BP", "12000BP", "6000BP", "2050", "2090"), 
     scales = list(draw=F)) + 
layer(sp.polygons(ice1)) + layer(sp.polygons(ice2)) 

levelplot with two polygons added, which are repeated across all panels

+0

Просьба воспроизводимый пример. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

ответ

1

Вот глупый пример, используя packets аргумент в latticeExtra::layer. Если бы вы захотели, вы могли бы просто добавить слой за слоем, как это.

library(lattice) 
library(latticeExtra) 
library(sp) 

Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2))) 
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2))) 
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5))) 
Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE) 

Srs1 = Polygons(list(Sr1), "s1") 
Srs2 = Polygons(list(Sr2), "s2") 
Srs3 = Polygons(list(Sr3, Sr4), "s3/4") 
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3) 

levelplot(rnorm(10, 10) ~ 1:10 + runif(10, 1, 10) | rep(c("A", "B"),each = 5)) + 
    layer(sp.polygons(SpP), packets = 1) 

Imgur

0

Вы можете использовать ggplot2 с rasterVis, это даст вам больше гибкости. facet_wrap, связанный с правильным параметром, позволит отделить многоугольники между видами растровых слоев. Для этого вам потребуется изменить столбец id вашего усиленного пространственного полигона, чтобы он соответствовал столбцу растра variable.
Вот воспроизводимый пример:

library(raster) 
library(rasterVis) 
library(ggplot2) 
library(sp) 

# Raster object 
fn <- system.file("external/test.grd", package="raster") 
s <- stack(fn, fn) 
names(s) <- c("A", "B") 

# Two polygons 
pol.A <- data.frame(x = c(179000, 179000, 180000, 180000), 
        y = c(330000, 331000, 331000, 330000)) 
pol.B <- data.frame(x = c(180000, 180000, 181000, 181000), 
        y = c(331000, 332000, 332000, 331000)) 
# Transform as Spatial Polygons to fit your example 
pol.sp <- SpatialPolygons(list(Polygons(list(Polygon(pol.A)), "A"), 
           Polygons(list(Polygon(pol.B)), "B"))) 
# Use Fortify to be able to use SpatialPolygons with ggplot2 
# rename id as "variable" to correspond to rasterstack gplot output 
pol.sp.fort <- fortify(pol.sp) %>% 
    rename(variable = id) 

# Plot. Note that gplot is not ggplot and issued from library rasterVis 
gplot(s) + 
    geom_tile(aes(fill = value)) + 
    geom_polygon(data = pol.sp.fort, aes(long, lat), col = "red") + 
    facet_wrap(~ variable)