Map plot incomplete when using rworldmap

Viewed 802

I have some data that I'd like to plot with rworldmap. Normally this works well. But I can't figure out why it's not plotting all the data when it says it's going to. Particularly it's not plotting data for the US.

I've got some data here: https://drive.google.com/file/d/1Fp7O2TRH5Blar56SqdRdcPh8Mb1Vb0pc/view?usp=sharing

And I'm running this code:

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

But then I'm getting this map: map plot

Notice how the US has no data. But it's definitely in the sample data. And it's only excluding one country, which is not the US.

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

Any idea what I'm doing wrong?

1 Answers

The problem is that you set the colourPalette and numCats parameters in a quite random fashion.

From your data we know exactly how many categories we have, and it can be counted with: length(table(mapData$change) and you need exactly that many colors (if you provide less then mapCountData will interpolate them with a warning).

Having said that, one solution of your problem is this

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

enter image description here

Related