This is an R Markdown document intended to show how to share interactive maps using leaflet. The idea is to fulfill the peer assignment revision scheduled in week 2 and start learning the use of leaflet library in R.
For the learning, practice purpose the dataset chosen for the excercise is located at http://www.fastfoodmaps.com/data.html, which contains data for several fast food restaurant accross the US territory.
Following there is shown the R code used and the final interactive map when chossing a particular state, then after clicking over the icon, address for selected location will popup.
Since the name of restaurant was not avaiable, fake names where choseen to classify among three kind of establishments: McDonald, KFC, Subway
# Loading used libraries
library(dplyr)
library(leaflet)
setwd("/Users/elobo/HomeRstudio/Curso9/W2Assig/")
# Getting raw data
thedata <- read.table(unz("fastfoodmaps_locations_2007.csv.zip","fastfoodmaps_locations_2007.csv"), sep=",")
MiniDF <- thedata %>% select(V3,V5,V8:V9)
colnames(MiniDF) <- c("Address", "State", "Latitud", "longitud")
# Generating names for each restaurant
Names <- sample(c("McDonald","KFC","Subway"), length(MiniDF$State), replace=TRUE)
MiniDF$Names <- Names
# Check locations for a particular State
StateInfo <- MiniDF %>% filter(State=="DC")
# Define icons to me mapped
SelIcon <- iconList(
McDonald = makeIcon("/Users/elobo/HomeRstudio/Curso9/W2Assig/Logos/mdlogo.png",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorX = 31*215/230/2,
iconAnchorY = 16),
KFC = makeIcon("/Users/elobo/HomeRstudio/Curso9/W2Assig/Logos/kfclogo.png",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorX = 31*215/230/2,
iconAnchorY = 16),
Subway = makeIcon("/Users/elobo/HomeRstudio/Curso9/W2Assig/Logos/swlogo.png",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorX = 31*215/230/2,
iconAnchorY = 16)
)
mylocations <- data.frame(lat=StateInfo$Latitud, lng=StateInfo$longitud)
mymap <- mylocations %>% leaflet() %>% addTiles() %>%
addMarkers(popup=StateInfo$Address, clusterOptions= markerClusterOptions(),
icon=SelIcon[Names]
)
mymap