O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required