library(dplyr)
library(leaflet)
load('data/yelp1.rdata')
# create popups in html format
url = "'http://www.cm.nsysu.edu.tw'"
biz$popup = paste(
sep="<br/>",
sprintf("<b><a href=%s>%s</a></b>",url,biz$name),
sprintf("Stars:%.1f, Reviews:%d",biz$star,biz$review),
biz$address)
# patition the biz data_frame by the no. review
biz$review %>% cut(c(0,5,10,50,100,200,400,900)) %>% table
.
(0,5] (5,10] (10,50] (50,100] (100,200] (200,400] (400,900]
5272 2406 2731 678 329 97 24
biz$no.review = cut(biz$review, c(0,5,10,50,100,200,400,900))
bx = split(biz, biz$no.review)
# create the map object
l <- leaflet() %>% addTiles()
# add a level of markers per partition
names(bx) %>% purrr::walk( function(df) {
l <<- l %>% addMarkers(
data=bx[[df]], lng=~longitude, lat=~latitude,
label=~name,
popup=~popup,
group = df,
clusterOptions=markerClusterOptions(removeOutsideVisibleBounds=F),
labelOptions = labelOptions(noHide=F, direction='auto'))
})
An Multi-Layer Interactive Map
Besides zoom-and-pan, try …
- clicking the check box in the upper right to selecting businesses (by the number of reviews)
- clicking the color buttons to zoomin
- hovering on the markers to see business names
- clicking on the markers to see the business info.
- URL,
- No. Stars,
- No. Review,
- Address
# plot the map
l %>% addLayersControl(
overlayGroups = names(bx),
options = layersControlOptions(collapsed = FALSE) )
LS0tDQp0aXRsZTogIuWkmuWxpOasoeS6kuWLleWcsOWcliIgDQpzdWJ0aXRsZTogIlllbHAgS2FnZ2xlIERhdGFzZXQgb24gdGhlIE1hcCINCmF1dGhvcjogIlRvbnkgQ2h1byINCmRhdGU6ICIyMDE35bm0N+aciDIz5pelIg0Kb3V0cHV0OiANCiAgaHRtbF9ub3RlYm9vazoNCiAgICBoaWdobGlnaHQ6IHRleHRtYXRlDQogICAgdGhlbWU6IGx1bWVuDQotLS0NCg0KYGBge3Igc2V0LW9wdGlvbnMsIGVjaG89RkFMU0UsIGNhY2hlPUZBTFNFfQ0KbGlicmFyeShrbml0cikNCm9wdGlvbnMod2lkdGg9MTAwKQ0Kb3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