Search code examples
rraster

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):

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):

For that I want to make some parts transparent and I set values below 700 to NaN (as NA didn't be transparent). 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). 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)

Solution

  • 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)