How to do it...

To plot geographic data using the sp package, follow these steps:

  1. Load the sp and rgdal packages:
> library(sp) 
> library(rgdal) 
  1. Read the data:
> countries_sp <- readOGR(".", "ne_50m_admin_0_countries") 
> airports_sp <- readOGR(".", "ne_50m_airports")
  1. Plot the countries without color:
> # without color 
> plot(countries_sp) 
  1. Plot the countries with color:
> # with color 
> plot(countries_sp, col = countries_sp@data$admin) 
 
  1. Add the airports. Do not close the previous plot:
> plot(airports_sp, add=TRUE) 
  1. Plot the economic ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.