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'))
})
Besides zoom-and-pan, try …
# plot the map
l %>% addLayersControl(
overlayGroups = names(bx),
options = layersControlOptions(collapsed = FALSE) )