R: How to display a partially bitmap on top of a map using ggplot2?

I want to draw parts of the raster (the highest values) on top of the previously drawn map. Here is my code:

ggplot() + geom_map(data = GERblue_iran, aes(map_id = state, fill = veggies), map = iran_states) + 
  expand_limits(x = iran_states$long, y = iran_states$lat) +      
  geom_polygon(data=iran_states, aes(x= long, y= lat, group=group), color='grey40', lwd=0.3, fill=NA) +
  scale_fill_distiller(palette="PuBu")

bringing this graph:

9CdnA.jpg

And the raster overlay code:

ggplot() + geom_tile(data=nut_iran.df, aes(x = x, y = y, fill = layer)) + scale_fill_distiller(palette = "Reds")

ANV2i.jpg

The expected result should look (more or less) like this (result made in photoshop !!):

KOkwz.jpg

shows only values ​​(raster) above +/- 1000.

Does anyone know how to do this in ggplot2?

For reproducibility, here is some of my data:

library(ggplot2)
# polygons
iran <- getData("GADM", country = "IRN", level = 1)
iran_states <- fortify(iran, region = "NAME_1")

# data for choropleth map: 
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), fruits = c(NA, 19645300.9396573, 
39318516.6693754, 15154130.3351692, 38374281.8287458, 29164989.9985857, 
125240289.719822, 6419392.00424945, NA, 23342736.5294504, 9223806.43987587, 
19008972.7788205, 62709223.291618, 41703691.9392781, 164306013.73773, 
13518682.0729514, 17420595.3934969, 44462391.2814304, 11715807.4374495, 
13475005.0070146, 12228946.6624824, NA, NA, 63708363.0757236, 
9221772.9477743, 13545791.4738047, 4268610.12809181, 6496251.74039526, 
31651316.7352119, 8570276.47057257, 10288282.5059752), nuts = c(NA, 
108771666.285736, 188713516.14938, 84626256.2539182, 227028948.551643, 
165167762.523232, 669060935.751113, 17905599.826691, NA, 124536243.958677, 
62369588.7036755, 123253859.776379, 3137384087.58166, 279956412.016931, 
506078060.03775, 70275261.7698334, 115596090.695869, 284469721.056207, 
73232219.7923014, 47691287.4633623, 73453936.1223698, NA, NA, 
382631908.316345, 78226462.4088062, 60449633.6571361, 25656409.607032, 
36523271.0224757, 233944364.555385, 158201233.377931, 74033085.0714528
), 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", "fruits", "nuts", "state"), row.names = c(NA, 
31L), class = "data.frame")

However, the raster dataset is too large, and I don’t know how to make a random raster font in this country. However, if someone knows how to do it. Here is the transformation I did to build it with geom_tile():

nut_iran.spdf <- as(nut_iran, "SpatialPixelsDataFrame")
nut_iran.df <- as.data.frame(nut_iran.spdf)

Thanks for your ideas.

EDIT: , @spacedman , , nut_iran.df$layer < 1000 . , na.value="transparent" scale_fill_distiller. :

X6jK0.jpg

:

39prs.jpg

- ( @hrbrmstr) (), NA choropleth .

+4

Source: https://habr.com/ru/post/1625312/


All Articles