如何创建R中的世界地图与特定国家填补?(How to create a world map in R

2019-06-25 19:42发布

我想用R生成一个非常基本的世界地图与一组特定的充满了红色的国家,以表明他们是疟疾流行的国家。

我有这些国家在数据帧的名单,但我在努力覆盖它们在世界地图上。

我已经使用尝试wrld_simpl对象并且还joinCountryData2Map在该方法中rworldmap包。

我将在这个答案发表评论,以防止增加了一个可能冗余的问题,但我不会在瞬间有足够的信誉,为这个道歉。

https://stackoverflow.com/a/9102797/1470099

我难以理解给予的参数plot()命令-我想知道是否有只是一个简单的方法来让R能够绘制所有的国名在我的名单上wrld_simpl地图,而不是使用grepl()等等等等。

plot(wrld_simpl, 
     col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])

Answer 1:

使用rworldmap包,你可以使用以下命令:

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

编辑:添加其他颜色,包括图片

## 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"))



Answer 2:

尝试使用googleVis包,然后用gvisGeoMap功能

G1 <- gvisGeoMap(Exports,locationvar='Country',numvar='Profit',options=list(dataMode='regions'))

plot(G1)


Answer 3:

    library(maptools)
    data(wrld_simpl)
    myCountries = wrld_simpl@data$NAME %in% c("Australia", "United Kingdom", "Germany", "United States", "Sweden", "Netherlands", "New Zealand")
    plot(wrld_simpl, col = c(gray(.80), "red")[myCountries+1])



文章来源: How to create a world map in R with specific countries filled in?