如何在R中创建填写了特定国家/地区的世界地图? [英] How to create a world map in R with specific countries filled in?
问题描述
我想用R生成非常基本的世界地图,其中用红色填充的一组特定国家/地区表明它们是疟疾流行国家/地区.
I would like to use R to generate a very basic world map with a specific set of countries filled with a red colour to indicate that they are malaria endemic countries.
我在数据框中列出了这些国家/地区,但正在努力将它们覆盖在世界地图上.
I have a list of these countries in a data frame but am struggling to overlay them on a world map.
我尝试使用wrld_simpl
对象以及rworldmap
软件包中的joinCountryData2Map
方法.
I have tried using the wrld_simpl
object and also the joinCountryData2Map
method in the rworldmap
package.
我会在这个答案上发表评论,以防止增加一个可能多余的问题,但是我目前没有足够的声誉,对此深表歉意.
I would comment on this answer to prevent addition of a possibly redundant question but I do not have enough reputation at the moment, apologies for this.
https://stackoverflow.com/a/9102797/1470099
我很难理解为plot()
命令提供的参数-我想知道是否有一种简单的方法告诉R在wrld_simpl
地图上的列表中标出列表中的所有国家/地区名称,而不是使用grepl()
等.
I am having difficulty understanding the arguments given to the plot()
command - I wondered if there was just an easy way to tell R to plot all of the country NAMEs in my list on the wrld_simpl
map instead of using grepl()
etc. etc.
plot(wrld_simpl,
col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])
推荐答案
使用rworldmap
包,您可以使用以下代码:
Using the rworldmap
package, you could use the following:
library(rworldmap)
theCountries <- c("DEU", "COD", "BFA")
# These are the ISO3 names of the countries you'd like to plot in red
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 1))
# malDF is a data.frame with the ISO3 country names plus a variable to
# merge to the map data
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
# This will join your malDF data.frame to the country map data
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8))
# And this will plot it, with the trick that the color palette's first
# color is red
添加其他颜色并添加图片
Add other colors and include picture
## Create multiple color codes, with Burkina Faso in its own group
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 2))
## Re-merge
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
## Specify the colourPalette argument
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8), colourPalette = c("red", "blue"))
这篇关于如何在R中创建填写了特定国家/地区的世界地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!