I'm having some trouble creating a world map with countries filled according to some variable with geom_map.
I want to create some world maps with country filled to reflect analysis outcomes. Country names are formatted differently in different places, so my analysis outcomes have a 3-letter ISO code associated with them. This code is also available in the high definition world map data from the rworldxtra package.
So I can set up my map data.frame thus:
library(rworldmap) library(rworldxtra) library(ggplot2) # high resolution so that I have all the islands I need and 3-letter ISO codes world_map<-getMap(resolution="high") robinson_world_map <- spTransform( world_map, CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs" ) ) # Convert to DF with 3-letter abbreviation as region id # -- this will link to my real data robinson_world_map_df <- fortify(robinson_world_map,region="ISO_A3")
If I just plot this with ggplot and geom_map, with no fill, it works fine (except for Warning: Ignoring unknown aesthetics: x, y):
ggworld <- ggplot() + geom_map( data=robinson_world_map_df, map=robinson_world_map_df, mapping=aes(map_id=id,x=long,y=lat), fill=NA, size=0.15, colour="black" ) + coord_fixed()
I would then use theme to make it a plain bakground with no axes, grid or borders.
For this example I use a dummy data data.frame:
data_df <- data.frame(id=unique(robinson_world_map_df$id)) data_df$value=rnorm(nrow(data_df))
I then try to plot my map with a fill based on data_df$value, ie with different data and map options to geom_map:
ggworld2 <- ggplot() + geom_map( data=data_df, map=robinson_world_map_df, mapping=aes( map_id=id,x=long,y=lat,fill=value), size=0.15, colour="black" ) + coord_fixed()
This gives the same warning about unknown aesthetics, but when I print ggworld2 I get an error message Error in eval(expr, envir, enclos) : object 'long' not found.
I'm currently using these versions of the software:
> R.version.string  "R version 3.3.2 (2016-10-31)" > installed.packages()[c("ggplot2","rworldmap","rworldxtra"),"Version"] ggplot2 rworldmap rworldxtra "2.2.1" "1.3-6" "1.01"
Any suggestions to fix my problem appreciated.
Any other suggestions/observations that would improve this (eg coord_proj on the ggplot call rather than spTransform before fortify) or might just be interesting are also welcome. Provided I can get them to work.