【问题标题】:How to create a world map in R with specific countries filled in?如何在 R 中创建一个填写特定国家的世界地图?
【发布时间】:2012-06-28 20:00:57
【问题描述】:

我想使用 R 生成一个非常基本的世界地图,其中包含一组特定的国家,用红色填充以表示它们是疟疾流行国家。

我在数据框中列出了这些国家/地区,但我很难将它们叠加在世界地图上。

我尝试过使用wrld_simpl 对象以及rworldmap 包中的joinCountryData2Map 方法。

我会对此答案发表评论,以防止添加可能多余的问题,但我目前没有足够的声誉,为此道歉。

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

我很难理解 plot() 命令的参数 - 我想知道是否有一种简单的方法可以告诉 R 在 wrld_simpl 地图上绘制我列表中的所有国家/地区名称,而不是使用 @ 987654327@等等等等。

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

【问题讨论】:

  • @ttmaccer,为什么不将其添加为答案?

标签: r maps maptools rworldmap


【解决方案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"))

【讨论】:

  • 非常感谢,这对我很有帮助……还有一些没有出现在地图上的非洲小国,但这将是另一天的任务!
  • 你知道做2组的可能性吗?即:红色的 c("DEU", "COD") 和蓝色的 c("BFA")?
  • @TeYaP,我刚刚添加了一个带有两种颜色的示例
  • 您知道我应该如何放大特定区域(例如欧洲或非洲)吗?
  • @AndreaM,您可以将mapCountryData 函数中的mapRegion 参数设置为"Africa" 或使用xlimylim 参数来设置地图范围。跨度>
【解决方案2】:

尝试使用 googleVis 包并使用 gvisGeoMap 函数

例如

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

plot(G1)

【讨论】:

  • 哇,我从来没有意识到 googleVis 有多么强大!非常简单的语法,不用担心spatialPointsDataFrame,输出很漂亮!
【解决方案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])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-09
    • 2021-06-05
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多