Below are the solutions to these exercises on plotting interactive maps with leaflet.
#################### # # # Exercise 1 # # # #################### library(leaflet) library(magrittr) m <- leaflet() %>% addTiles() # Add default OpenStreetMap map tiles m # Print the map #################### # # # Exercise 2 # # # #################### library(leaflet) library(magrittr) m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") m # Print the map #################### # # # Exercise 3 # # # #################### library(leaflet) library(magrittr) df = data.frame(Lat = 1:10, Long = rnorm(10)) #################### # # # Exercise 4 # # # #################### library(leaflet) library(magrittr) # add some circles to a map df = data.frame(Lat = 1:10, Long = rnorm(10)) leaflet(df) %>% addCircles() #################### # # # Exercise 5 # # # #################### library(leaflet) library(magrittr) leaflet(df) %>% addCircles(lng = ~Long, lat = ~Lat) #################### # # # Exercise 6 # # # #################### library(leaflet) library(magrittr) leaflet() %>% addCircles(data = df) leaflet() %>% addCircles(data = df, lat = ~ Lat, lng = ~ Long) #################### # # # Exercise 7 # # # #################### library(leaflet) library(magrittr) library(maps) mapStates = map("state", fill = TRUE, plot = FALSE) leaflet(data = mapStates) %>% addTiles() %>% addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) #################### # # # Exercise 8 # # # #################### library(leaflet) library(magrittr) library(maps) mapStates = map("state", fill = F, plot = T) leaflet(data = mapStates) %>% addTiles() %>% addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) #################### # # # Exercise 9 # # # #################### library(leaflet) library(magrittr) m = leaflet() %>% addTiles() df = data.frame( lat = rnorm(100), lng = rnorm(100), size = runif(100, 5, 20), color = sample(colors(), 100) ) m = leaflet(df) %>% addTiles() m %>% addCircleMarkers(radius = ~color, color = ~size, fill = T) m %>% addCircleMarkers(radius = runif(100, 4, 10), color = c('red')) #################### # # # Exercise 10 # # # #################### library(leaflet) library(magrittr) m = leaflet() %>% addTiles() df = data.frame( lat = rnorm(100), lng = rnorm(100), size = runif(100, 5, 20), color = sample(colors(), 100) ) m = leaflet(df) %>% addTiles() m %>% addCircleMarkers(radius = ~size, color = ~color, fill = F) m %>% addCircleMarkers(radius = runif(100, 4, 10), color = c('GREEN'))
Leave a Reply