A short tutorial on plotting world maps in R.

Install and load packages

You will need rworldmap and ggplot2, which is part of the tidyverse package. We will plot some gapminder data, so will need that package too:

library(rworldmap)
library(tidyverse)
library(gapminder)

Load map data

This function will give you the data for plotting each country:

map.all <- map_data(map = "world")
head(map.all) ## region denotes the country
##        long      lat group order region subregion
## 1 -69.89912 12.45200     1     1  Aruba      <NA>
## 2 -69.89571 12.42300     1     2  Aruba      <NA>
## 3 -69.94219 12.43853     1     3  Aruba      <NA>
## 4 -70.00415 12.50049     1     4  Aruba      <NA>
## 5 -70.06612 12.54697     1     5  Aruba      <NA>
## 6 -70.05088 12.59707     1     6  Aruba      <NA>

Get your own data in order

Your data need to match the country names in map.all. This is probably what will take you longest to sort out, but we will use some good enough data today:

head(gapminder) ## This is the gapminder dataset
## # A tibble: 6 x 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.
gapminder <- rename(gapminder, region = country) ## we need to rename column country to region, so we can merge gapminder and map.all

Merge datasets

Then merge the two dataframes map.all and all:

map.all <- left_join(map.all, gapminder, by = "region") 
head(map.all)
##        long      lat group order region subregion continent year lifeExp pop
## 1 -69.89912 12.45200     1     1  Aruba      <NA>      <NA>   NA      NA  NA
## 2 -69.89571 12.42300     1     2  Aruba      <NA>      <NA>   NA      NA  NA
## 3 -69.94219 12.43853     1     3  Aruba      <NA>      <NA>   NA      NA  NA
## 4 -70.00415 12.50049     1     4  Aruba      <NA>      <NA>   NA      NA  NA
## 5 -70.06612 12.54697     1     5  Aruba      <NA>      <NA>   NA      NA  NA
## 6 -70.05088 12.59707     1     6  Aruba      <NA>      <NA>   NA      NA  NA
##   gdpPercap
## 1        NA
## 2        NA
## 3        NA
## 4        NA
## 5        NA
## 6        NA

Now you can make the plot:

ggplot() + geom_map(data = map.all, map = map.all, 
                    aes(map_id = region,             ## map_id is the variable name of the countries,
                        x = long, y = lat, 
                        fill = pop)) +               ## fill will colour in each country
                                                     ## Use colour = varname to change colour of the outline
  scale_fill_gradientn(colours=terrain.colors(5)) +  ## this will change the colours in the gradient
  labs(fill = "Population size") +                   ## changes the name of the legend
  theme_classic() +                                  ## change the overall look of the plot
  theme(panel.background = element_rect(colour = "black", fill = "NA"), 
        axis.ticks.length = unit(.05, "cm"), 
        text = element_text(size = 16.5))            ## change some more of the defaults