In this I tried to map the places I vistied during my academics & job using the leaflet R package. Packages used: XML,plyr & leaflet
Install the above packages and load them
Preparing the data
library(XML)
library(plyr)
library(leaflet)
# Preparing the data
# character vector cities
cities <- c("Varanasi, India", "Jaipur, India", "Residency Road, Bangalore, India" ,"ITPL, Bangalore, India", "Phoenix, USA",
"Guadalajara, Mexico", "Marthahalli, Bangalore, India", "Melbourne, Australia","Bangalore, India")
# character vector description
desc = c("School @ St. John's School", "Graduation @ SKIT, Jaipur", "Job @ Think Ahead Advisory Services Ltd.",
"Job @ TCS Bang.", "Job @ TCS Phoenix", "Job @ TCS Mexico", "Job @ NICE Interactive Solutions", "Job @ NICE Systems Aus.","Job @ NICE Interactive Solutions")
We do not have the geocodes so will be using the Google’s map API to fetch the geocodes for places visited.
Let us make a function which would be called for each row in the character vector and return a dataframe with the place, correspoding latitude and longitude.
# Function to get the latitude and longitude
latlong <- function(place)
{
theURL <- sprintf('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=%s', place)
doc <- xmlToList(theURL)
data.frame(Place=place,
Latitude=as.numeric(doc$result$geometry$location$lat),
Longitude=as.numeric(doc$result$geometry$location$lng),
stringsAsFactors=FALSE)
}
Call the above function on the character vector cities. Note Google has an upper cap on how many geocodes you can fetch in a day.
places <- adply(cities,1,latlong)
str(places)
## 'data.frame': 9 obs. of 4 variables:
## $ X1 : Factor w/ 9 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9
## $ Place : chr "Varanasi, India" "Jaipur, India" "Residency Road, Bangalore, India" "ITPL, Bangalore, India" ...
## $ Latitude : num 25.3 26.9 13 13 33.4 ...
## $ Longitude: num 83 75.8 77.6 77.7 -112.1 ...
Make a dataframe with cities, lat , long and description to be used in maping
df = data.frame(places, desc, stringsAsFactors = F)
str(df)
## 'data.frame': 9 obs. of 5 variables:
## $ X1 : Factor w/ 9 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9
## $ Place : chr "Varanasi, India" "Jaipur, India" "Residency Road, Bangalore, India" "ITPL, Bangalore, India" ...
## $ Latitude : num 25.3 26.9 13 13 33.4 ...
## $ Longitude: num 83 75.8 77.6 77.7 -112.1 ...
## $ desc : chr "School @ St. John's School" "Graduation @ SKIT, Jaipur" "Job @ Think Ahead Advisory Services Ltd." "Job @ TCS Bang." ...
Using leaflet for maping
mymap = leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addProviderTiles("CartoDB.DarkMatter",
options = providerTileOptions(opacity = 0.8)) %>%
setView(5, 20, zoom=1) %>%
# color and weight arguments for the color and weight of the line
addPolylines(data=df, ~Longitude, ~Latitude, color = "white",
weight = 2, opacity = 0.6) %>%
addCircleMarkers(data=df, ~Longitude, ~Latitude, color = "#daa520",
opacity = 0.6, radius = 2, popup = paste(cities, desc, sep="<br>"))
mymap
Pan and zoom can be adjusted
References: Blog post from Jared Lander