使用 rworldmap 时地图绘制不完整

Map plot incomplete when using rworldmap

我有一些数据想用 rworldmap 绘制。通常这很有效。但是我不明白为什么它说要绘制时没有绘制所有数据。特别是它没有为美国绘制数据。

我这里有一些数据:https://drive.google.com/file/d/1Fp7O2TRH5Blar56SqdRdcPh8Mb1Vb0pc/view?usp=sharing

我是 运行 这个代码:

mergedData = readRDS("sampleData.rds")
changeHeatMapPalette = c('#D7191D', '#FDAE61', '#FFFFBF', '#ABD9E9', '#2C7BB6')
mapData = joinCountryData2Map(mergedData, joinCode="ISO2", nameJoinColumn="country", mapResolution = "high")
mapCountryData(mapData, nameColumnToPlot="change", mapTitle="", catMethod = "diverging", colourPalette = changeHeatMapPalette, numCats = 90, borderCol = "grey70")

但后来我得到了这张地图:

注意美国没有数据。但它肯定在示例数据中。而且它只排除了一个国家,不是美国。

108 codes from your data successfully matched countries in the map
1 codes from your data failed to match with a country code in the map
     failedCodes
[1,] "GF"       
143 codes from the map weren't represented in your data

知道我做错了什么吗?

问题在于您以相当随机的方式设置了 colourPalettenumCats 参数。

根据您的数据,我们确切地知道我们有多少类别,可以用以下方法计算:length(table(mapData$change) 并且您需要那么多颜色(如果您提供的颜色少于 mapCountData 将对它们进行插值警告)。

话虽如此,您的问题的一种解决方案是

mapCountryData(mapData, 
  nameColumnToPlot="change", 
  mapTitle="", 
  catMethod = "diverging", 
  colourPalette = brewer.pal(library(RColorBrewer), 'RdYlBu'), 
  numCats = length(table(mapData$change)), 
  borderCol = "grey70")