简体   繁体   中英

How to select specific countries to make a choropleth map

I'm trying to make a choropleth map of some specific countries. I have the data with values for the choropleth map, and the other countries that I don't have data will be plotted in grey.

So here is my code until now:

data_iso = data.frame(region=c("Argentina", "Brazil",   "Chile", "Colombia", "Costa Rica", "Mexico", "Panama", "Peru",  "El Salvador"),
                      ratemort = c(25.510391, 73.875825,20.297896,  73.557939  ,23.266236,  42.190321,  48.069608,  9.971303 ,109.941822))

library(ggmap)
library(ggplot2)
library(raster)
library(maptools)

mapa <- borders("world", regions = c("Brazil", "Uruguay", "Argentina", "French Guiana", "Suriname", "Colombia", "Venezuela",
                                     "Bolivia", "Ecuador", "Chile", "Paraguay", "Peru", "Guyana", "Panama", "Costa Rica", 
                                     "Nicaragua", "Honduras", "El Salvador", "Belize", "Guatemala", "Mexico", "Trinidad and Tobago",
                                     "Caribe", "Puerto Rico", "Dominican Republic", "Haiti", "Jamaica", "Cuba", "Bahamas", "Antiles",
                                     "Dominica", "Saba"), 
                fill = "grey70", colour = "black")
ggplot() + mapa + theme_bw() + xlab("Longitude (decimals)") + ylab("Latitude (decimals)") + 
  theme(panel.border = element_blank(), panel.grid.major = element_line(colour = "grey80"), panel.grid.minor = element_blank())

在此处输入图像描述

Then, I'd like to add the values of data_iso$ratemort in the their respective countries.

Any suggestion?

I am not exactly sure what your question is. But since you used the choroplethr tag I will explain how to make the map using the choroplethr package:

data_iso = data.frame(region=c("argentina", "brazil",   "chile", "colombia", "costa rica", "mexico", "panama", "peru",  "el salvador"),
                      value = c(25.510391, 73.875825,20.297896,  73.557939  ,23.266236,  42.190321,  48.069608,  9.971303 ,109.941822))

country_choropleth(data_iso, zoom=data_iso$region)

在此处输入图像描述

The key modification is this. The function ?country_choropleth , like all functions in the choroplethr suite of packages, requires your dataframe to have one column named region and one column named value . Also, all regions in choroplethr generally use lowercase. See ?country_choroplethr for details on naming conventions.

Your map has no color. I am not sure if that is the error that you want help with.

By default choroplethr uses Black to convey NA values. Here I set the zoom to be only countries that have data. I do not know if all the counties in your zoom list are present in the map, if they are spelled in the same way they appear in the map, and so on. See the zoom parameter in ?country_choropleth for details. Getting the naming right is normally the hardest part of mapping in R (including the choroplethr package).

If you want to use ggplot, you need to add your dataframe to ggplot and add fill in aes with your column so that the map fills the colors based on the values of the ratemort.

Try something like this:

ggplot(data = data_iso, aes(fill = ratemort)) + mapa + theme_bw() +  
 xlab("Longitude (decimals)") + ylab("Latitude (decimals)") +  
 theme(panel.border = element_blank(), panel.grid.major =  
 element_line(colour = "grey80"), panel.grid.minor = element_blank())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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