Specalize in Geo-Spatial Visualizations With Leaflet – Part 1: Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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 geo-spatial data of the 26 cantons of Switzerland (more information about Switzerland cantons here.) The data is readily available in the leaflet package under the variable leaflet::gadmCHE. 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
The first steps would be to set-up the map “scaffolding” so there’s still no need to use the data.
Create a leaflet map and select provider tiles of your choice, but not the default ones.
Hint: available provider tiles can be viewed here.

Exercise 2
Set the default view to the coordinates of the center of Switzerland with zoom level 7.
Hint: use Google to find the relevant coordinates.

Exercise 3
Restrict the zooming options to minimum level 5 and maximum level 9.

Exercise 4
Use our data-set leaflet::gadmCHE to draw the borders of the cantons.

Exercise 5
Change the border and the fill colors to the Swiss national flag colors (red and white, respectively.)
In addition, change the opacity and the weight of the border lines according to your aesthetic preferences.

Exercise 6
The capital city of Switzerland, “Bern”, is located in a canton with the same name.
Add a marker that indicates the center of the “Bern” canton.
Hint: the coordinates are available in the labpt slot of the polygons data.

Exercise 7
Upon hovering over a canton, change the borders of that canton to dark red.

Exercise 8
Upon hovering over a canton, show a label with its name.

Exercise 9
Add a distance/area measurement functionality to the map.
The measurment units should be kilometers (for distances) and square-meters (for areas.)

Exercise 10
Add the mini-map feature to your map.
The mini-map should have the same provider tiles and it should start minimized.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)