R Beginners – Plotting Locations on to a World Map
[This article was first published on Doodling with Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post is targeted at those who are just getting started plotting on maps using R.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The relevant libraries are: maps, ggplot2, ggmap, and maptools. Make sure you install them.
The Problem
Let’s take a fairly simple use case: We have a few points on the globe (say Cities) that we want to mark on the map.The ideal and natural choice for this would be David Kahle’s ggmap package. Except that there is a catch. ggmap doesn’t handle extreme latitudes very well. If you are really keen on using ggmap, you can do it by following the technique outlined in this StackOverflow response.
If ggmap is not mandatory, there are simpler ways to do the same.
First, let’s set up our problem. We’ll take 5 cities and plot them on a world map.
Method 1: Using the maps Package
This results in:
Which might be enough. However, if you take the few extra steps to plot using ggplot, you will have much greater control for what you want to do subsequently.
Method 2: Plotting on a World Map using ggplot
This results in:
To leave a comment for the author, please follow the link and comment on their blog: Doodling with Data.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.