开发者

How to block a part of the level plot in R made using lattice package?

开发者 https://www.devze.com 2023-04-07 19:59 出处:网络
I have made a level plot in R of a variable using the lattice package. This grid corresponds to South Asia. I am only interested in viewing the values of this variable (aerosol optical depth) for cert

I have made a level plot in R of a variable using the lattice package. This grid corresponds to South Asia. I am only interested in viewing the values of this variable (aerosol optical depth) for certain countries in South Asia. I have a dummy variable that takes the value 1 for the countries I am interested开发者_运维技巧 in and 0 otherwise. Is it possible for me to colour this part of the grid black or any other colour?

I cannot show the level plot as I am low on reputation with stackoverflow. (The pdf that was attached to the crossposted message to rhelp should now appear:)

Here is my R code:

levelplot(aod ~ longitude + latitude | factor(day), data = aod_Jan, 
    aspect="iso", contour = TRUE, layout=c(1,1))

How to block a part of the level plot in R made using lattice package?


Since you are using geographical data, maybe the raster package is useful for you. For example, let's display the altitude of France (download this zip file or use the raster::getData function). After you unzip the file:

library(raster)
fraAlt <- raster('FRA_alt')
plot(fraAlt) ## Not only France is displayed...

If you want to display only the altitude of France, you need the information of the boundaries: download this RData file (or use the raster::getData function). This RData contains a SpatialPolygonsDataFrame (named gadm) which can be converted to a Raster with:

 mk <- rasterize(gadm, fraAlt)

Now you can mask the altitude raster with the boundaries:

 fraAltMask <- mask(fraAlt, x)
 plot(fraAltMask) ##Now only France is displayed

Finally, if you want to use lattice methods you need the rasterVis package:

 library(rasterVis)
 levelplot(fraAlt)
 levelplot(fraAltMask)

Now, all together with the boundaries superimposed:

 s <- stack(fraAlt, fraAltMask)
 layerNames(s) <- c('Alt', 'AltMask')
 boundaries <- as(gadm, 'SpatialLines')
 levelplot(s) + layer(sp.lines(boundaries))


Use the subset argument to levelplot. Perhaps:

levelplot(aod ~ longitude + latitude | factor(day), data = aod_Jan, subset = dummy==1,
    aspect="iso", contour = TRUE, layout=c(1,1))
0

精彩评论

暂无评论...
验证码 换一张
取 消