将数据与SpatialPolygonsDataFrame对象列表进行匹配,以在传单中运行等值区域映射

时间:2018-01-06 01:49:45

标签: r maps leaflet sp choropleth

我自己教导如何在R中使用绘图工具,在这种情况下,我希望根据{{{{{{{{{{{{{{{{{{{ 1}}下面的数据框。

请注意,我实际上是在30个国家/地区运行此功能。我只是为了重现目的而限制在2个国家/地区。另请注意,mydata功能将从GADM.org网站下载。

getData

我现在想要做的是将library(sp) library(raster) library(tidyverse) library(leaflet) France_map <- getData(name = "GADM", country = "FRA", level = 1) Germany_map <- getData(name = "GADM", country = "DEU", level = 1) all_maps <- list(France_map, Germany_map) mydata <- dput(structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Germany", "France"), class = "factor"), region_name = structure(1:37, .Label = c("Baden-Württemberg", "Bayern", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern", "Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland", "Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen", "Île-de-France", "Champagne-Ardenne", "Picardie", "Haute-Normandie", "Centre", "Basse-Normandie", "Bourgogne", "Nord-Pas-de-Calais", "Lorraine", "Alsace", "Franche-Comté", "Pays de la Loire", "Bretagne", "Poitou-Charentes", "Aquitaine", "Midi-Pyrénées", "Limousin", "Rhône-Alpes", "Auvergne", "Languedoc-Roussillon", "Provence-Alpes-Côte d'Azur"), class = "factor"), prop = c(0.0771812080536913, 0.0808625336927224, 0.154200131820421, 0.143712574850299, 0.125, 0.1875, 0.160220994475138, 0.201612903225806, 0.149122807017544, 0.140562248995984, 0.111111111111111, 0.130434782608696, 0.162454873646209, 0.141891891891892, 0.164835164835165, 0.0810810810810811, 0.320361006883289, 0.173944002182893, 0.267984998166555, 0.145696294975402, 0.307927600677265, 0.218154962499145, 0.249630808322935, 0.276741747762545, 0.0959323496890737, 0.144213110146746, 0.0929990840802643, 0.190088309954592, 0.159029556704208, 0.204186267910012, 0.26199124557682, 0.171309738332821, 0.125707503945437, 0.241559205433323, 0.170811734710593, 0.275876128675216, 0.205483749159249)), .Names = c("cntry", "region_name", "prop"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -37L))) prop变量的结果显示在等值区域图上,使用类似的内容:

mydata

我的绊脚石是,如何让R匹配leaflet() %>% addPolygons() 变量中的结果到SPDF文件中相应的区域来运行等值区域图?

1 个答案:

答案 0 :(得分:3)

我合并了Germany_mapFrance_map,以便我可以一次绘制多边形。然后,我为prop创建了颜色。使用addPolygons()时,请指定fillColor = ~mypal(mydata$prop)。 prop值不必保留在多边形数据中。同样,您在values = mydata$prop中指定addLegend()。您将下面的结果视为屏幕截图。

library(sp)
library(raster)
library(maptools)
library(leaflet)

# Combine German and French polygons
mymap <- rbind(Germany_map, France_map, makeUniqueIDs = TRUE)

### Create colors for prop
mypal <- colorNumeric(palette = "Reds", domain = mydata$prop)


leaflet() %>% 
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lat = 49.2402, lng = 6.9969, zoom = 6) %>%
addPolygons(data = mymap,
            stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.5,
            fillColor = ~mypal(mydata$prop),
            popup = paste("Region: ", mymap$NAME_1, "<br>",
                          "Prop: ", mydata$prop, "<br>")) %>%
addLegend(position = "bottomright", pal = mypal, values = mydata$prop,
          title = "Prop",
          opacity = 0.5)

enter image description here