Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Leaflet is a JavaScript library for interactive maps. It is widely used across many platforms and fortunately, it is also implemented as a very user-friendly R package! With leaflet, you can create amazing maps within minutes that are customized exactly to your needs and embed them within your Shiny apps, markdowns or just view them in your RStudio viewer.
In the following set of exercises, we will use the geo-spatial data of the Atlantic Ocean storms in 2005. The data is readily available in the leaflet package under the variable leaflet::atlStorms2005
. Each exercise is adding some more features/functionalities to the code of the previous exercise, so be sure not to discard the code until after you’re done with all of the exercises. Answers to these exercises are available here.
For other parts of the series, follow the tag leaflet.
Exercise 1
Set the default view to longitude -47.4, latitude 39.75 and zoom level 3.
In addition, add a button that will scale back to the default view upon clicking it.
Hint: use the icon()
function from the shiny package to easily render an icon of your choice for the button.
Exercise 2
Add the lines that represent the storms traces to the map.
Exercise 3
Color each line according to the storm max wind.
Exercise 4
Add a legend to the colors you just added.
Exercise 5
Upon hovering over a line, change its weight to 10.
Exercise 6
Upon hovering over a line, show a label with the storm name.
Exercise 7
Upon clicking a line, show a pop-up with the storm minimum pressure.
Exercise 8
Save your leaflet map as an HTML file.
Hint: use htmlwidgets::save()
.
Related exercise sets:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.