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.
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.
Set the default view to the coordinates of the center of Switzerland with zoom level 7.
Hint: use Google to find the relevant coordinates.
Restrict the zooming options to minimum level 5 and maximum level 9.
Use our data-set
leaflet::gadmCHE to draw the borders of the cantons.
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.
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.
Upon hovering over a canton, change the borders of that canton to dark red.
Upon hovering over a canton, show a label with its name.
Add a distance/area measurement functionality to the map.
The measurment units should be kilometers (for distances) and square-meters (for areas.)
Add the mini-map feature to your map.
The mini-map should have the same provider tiles and it should start minimized.
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below