This was an assignment I took up as a PoC for a friend. A friend asked me if I could find the distance between two points on a map in R. A while of web search and trying out some code, I finally found some really cool tricks with maps in R using the ggmap package. I’d like to thank Robin who came to me with the idea and Anjali Doney, whose blog post for the Fastah Project has been of great use to create case study 1 below. I would also like to thank the Fastah Project for the FastahDatasetMapsTutorial dataset.
This tutorial does not explore the entire ggmap package but walks you through enough content to get an idea of using ggmaps and using maps from Google and stamen. Knowledge of ggplot2 might help add creativity to your maps but you could very well follow the instructions below.
You might want to confirm you have the ggplot2 package installed. It is a dependency for ggmap and R should take care of it, in case you have never installed ggplot2 in the past.
to install ggmap type:
call the packages to get started:
We can start by importing some maps from Google and Stamen and observe how R streams in data and plot it.
We also get other views as shown below. You can adjust the zoom level by passing it as an argument to the qmap function.
qmap("Bangalore", zoom = 7, color = "bw")
We have alternate sources that can be used in the qmap function. Use one of the below to fetch maps from Stamen. The first map shows the water bodies in Bangalore/Bengaluru:
qmap(location = "bangalore",maptype="watercolor")
qmap(location = "bangalore",maptype="watercolor", source = "stamen")
qmap(location = "Bangalore", maptype="toner-lite")
The toner lite map from stamen is a lightweight ggplot friendly map which can be used for creating visualizations. You will see more of this map below.
Case Study 1: Visualizing points on maps using ggplot and ggmap
In our first Case study, we will see how ggmap works with ggplot to produce stunning visualizations on maps. We use the Fastah dataset which is a collection of latitude and longitude of mobile networks and details of the network operator and reception or performance.
library(readr) df <- read_csv("https://raw.githubusercontent.com/fastah/sample-data/master/FastahDatasetMapsTutorial.csv") library(ggplot2) map1<-qmap("Bangalore", zoom = 11, maptype="toner-lite") map1 + geom_point(data = df, aes(lon,lat,color=Operator),size=2,alpha=0.7) + labs(x = "Longitude", y = "Latitude", title="Ping Locations", color = "Operator")
map1 + geom_point(data = df, aes(lon,lat,color=Class),size=2,alpha=0.8) + scale_color_manual(values=c("#66FF99", "#FF0060", "#FFB700"))+ labs(x = "Longitude", y = "Latitude", title="Ping Locations", color = "Reception")
Case Study 2: Routing between two points
Here we try to find the route between Indiranagar and Murgeshpalya in Bangalore. We first fetch the route coordinates from Google. I used from Indiranagar, Bangalore to Murgeshpalya here.
Lets try plotting this path on Google maps.
BangaloreMap = qmap("Indiranagar, Bangalore", zoom = 13) BangaloreMap + geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), color = "red", data = rt1)
We can also do this on a toner-lite map.
BangaloreMap_tl = qmap("Indiranagar, Bangalore", zoom = 13, maptype="toner-lite") BangaloreMap_tl + geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), color = "red", data = rt1)
We can also find the distance between these points using the distance function in the ggmap package.
distance<-mapdist(from = "Indiranagar, Bangalore", to = "Murgeshpalya, Bangalore", mode = "driving", output = "all") distance$`Indiranagar, Bangalore`$`Murgeshpalya, Bangalore`$distance$text distance$`Indiranagar, Bangalore`$`Murgeshpalya, Bangalore`$duration$text
You could use this package for routing or making some stunning visualizations on a shiny dashboard. Happy Coding!!!