简体   繁体   English

R:如何通过在基础R图中用栅格过度绘制地图来获得正确的分辨率(栅格)?

[英]R: How to get the right resolution (raster) by overplotting a map with raster in base R plot?

I want to overlay a choropleth map (created in base R plot): 我想覆盖一个Choropleth贴图(在基本R图中创建):

code: 码:

library(classInt)
library(RColorBrewer)
library(raster)

GERblue_iran <- structure(list(veggies = c(NA, 1135142.7169744, 1064475.14405642, 
579007.139090945, 2291173.06203667, 1609487.86612194, 5514745.42173307, 
210033.193615536, NA, 1082275.82518455, 395053.664034339, 833546.886449334, 
1350410.79594876, 2030498.45168616, 5018327.9046678, 413119.296060151, 
853322.135586823, 2136776.14200603, 581494.047168068, 535593.624579909, 
414310.523642145, NA, NA, 2156369.86690811, 274390.590608389, 
546804.909031463, 144406.95766963, 285002.432443622, 1605244.30546598, 
307546.827903725, 589330.238261654), state = c("Alborz", "Ardebil", 
"Bushehr", "Chahar Mahall and Bakhtiari", "East Azarbaijan", 
"Esfahan", "Fars", "Gilan", "Golestan", "Hamadan", "Hormozgan", 
"Ilam", "Kerman", "Kermanshah", "Khuzestan", "Kohgiluyeh and Buyer Ahmad", 
"Kordestan", "Lorestan", "Markazi", "Mazandaran", "North Khorasan", 
"Qazvin", "Qom", "Razavi Khorasan", "Semnan", "Sistan and Baluchestan", 
"South Khorasan", "Tehran", "West Azarbaijan", "Yazd", "Zanjan"
)), .Names = c("veggies", "state"), class = "data.frame", row.names = c(NA, 
31L))

iran2 <- getData("GADM", country = "IRN", level = 1)
iran2$veggies <- GERblue_iran[,1]
iran2$veggies[is.na(iran2$veggies)] <- 0
cols <- brewer.pal(n = 4, name = "PuBu")
# jenks
lcols2 <- cut(iran2$veggies,
             breaks = classIntervals(iran2$veggies, n=7,style='jenks')$brks,
             labels = cols)
x11()
plot(iran2, col = as.character(lcols2))

This map should be overlay by this RasterLayer (sorry to big to attach): 该地图应通过此RasterLayer覆盖(很抱歉,无法附加):

For that I want to make some parts transparent and I set values below 700 to NaN (as NA didn't be transparent). 为此,我想使某些零件透明,然后将NaN的值设置为700以下(因为NA不透明)。 Resulting in this: 结果是:

As you can see single values are plotted in the bottom part of the map. 如您所见,单个值绘制在地图的底部。 However, the size (or resolution) of the dots/pixels is very coarse compared to the original raster (this occur also if I just plot without NaN and transparency). 但是,与原始栅格相比,点/像素的大小(或分辨率)非常粗糙(如果我只是在没有NaN和透明度的情况下绘图,也会发生这种情况)。 Does anyone know how to set the resolution wright in order to have the original size? 有谁知道如何设置分辨率以达到原始尺寸? Thanks 谢谢

Here the code with both plots: 这两个代码的代码:

plot(iran2, col = as.character(lcols2)); plot(nut_iran2, add=TRUE)

Here is how you can make your example much simpler: 这是使示例更简单的方法:

library(raster)

iran <- getData("GADM", country = "IRN", level = 1)
r <- raster(iran, nrow=100, ncol=100)
set.seed(-99)
values(r) <- runif(ncell(r))
r <- mask(r, iran)
r[r < 0.99] <- NA

And here is how you can make the plot: 这是绘制情节的方法:

plot(r)
plot(iran, col = rainbow(6, start=0.5, end=0.65), add=TRUE)
plot(r, add=TRUE, legend=FALSE)

Because raster plotting uses a legend you may need to set up the plot area first. 由于栅格绘图使用图例,因此您可能需要首先设置绘图区域。 But you can also do 但是你也可以

plot(iran, col = rainbow(6, start=0.5, end=0.65))
plot(r, add=TRUE)

or 要么

plot(iran, col = rainbow(6, start=0.5, end=0.65), axes=TRUE)
plot(r, add=TRUE, legend=FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM