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 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.