Plotting global data by countries on a world map

In this recipe, we will learn how to plot country-wise data on a world map.

Getting ready

We will use a few different additional packages for this recipe. We need the maps package for the actual drawing of the maps, the WDI package to get the World Bank data by countries, and the RColorBrewer package for color schemes. So, let's make sure these packages are installed and loaded:

install.packages("maps")
library(maps)
install.packages("WDI")
library(WDI)
install.packages("RColorBrewer")
library(RColorBrewer)

How to do it...

There are a lot of different data we can pull in using the world bank API provided by the WDI package. In this example, let's plot some CO2 emissions data:

colours = brewer.pal(7,"PuRd") ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.