Leaflet is perhaps the most full-featured htmlwidget for interactive maps. It's developed by members of RStudio and supports a wide range of advanced functionality (SHOW). Leaflet does require an Internet connection.
- [Instructor] The Leaflet library allows us to create a whole host of different interactive maps. Let's see some of the things that we can do with Leaflet. In our exercise files, I'll load example_maps.R. Just decrease the size of my environment because I don't need it. I'll load the quiz-it packages at the top of the script file with Command, Shift, Enter. It's really easy to create scatter geo marker plots in Leaflet, so I'll do that with lines six through nine. Scatter geo marker plots are really useful to say here are a number of cities spread across the United States, Europe, and Australia, but how about if you need to demonstrate the relative importance of the locations? Or how a variable ranges between these locations.
Leaflet provides tools for making scattered geo plots with the size of circles providing some information about the relative values between cities. This code is given on line 12 through 16, and we can see here that I have a city inside of Germany which is really quite large compared to all of this. One really cool thing that we can do with scattered geo plots in Leaflet is we can cluster together the markers. Let's have a look at what a code 19 through 24 achieves. If I hit Command, Enter to evaluate that.
At a global level, we can see 100 markers within the United States of America and as I zoom in, we can see these break apart and I can see there are more observations in New York but I could zoom in if I so wished. Also, it's really easy to build choropleth within Leaflet. If I'm going to code on line 27 and line 28 through 35, we can see I'm generated a choropleth of the world split by continent.
It's also possible to use Leaflet within Shiny apps. Let's demonstrate that by going to Files, loading up the server.R file and then I'll run my app by clicking Run App. What my Shiny App allows me to do is to get click coordinates. So, if I click on Australia then it tells me I selected Australia and the exact longitude and latitude of my click position, but it's also possible for me to remove countries. So, I could remove my home country, the United Kingdom if I so wished, and I could also remove Antarctica.
So, how does Leaflet compare to other options for visualizing maps? Well, Leaflet is explicitly designed for plotting geographic features which means we really can't use it for any other types of chart, but because Leaflet is designed for arbitrary geographic features, we can do some pretty impressive things like creating representative choropleth. Let me show you an example in RStudio. There's an excellent example in the arbitrary geographic features dot R file which is taken from this file here on R pubs.
Let's run all of the code by pressing Command A and Command Enter. And we can see here that I have a wonderful hexagonal representation of the states of the United States of America. This is simply not possible in other htmlwidget libraries. Because of Leaflet's ability to work with arbitrary geographic features, I'm very comfortable in recommending Leaflet as the single best option for creating interactive maps, no matter what kind of map you want to build.
Plus, unlike other libraries, one cannot simply click on legend items to add or remove series from a chart. There is, however, functionality to add, remove layers within a Leaflet htmlwidget map, but I would say, it's generally simpler to use Shiny to add complex interactivity to Leaflet maps if you're able to build a Shiny app. So how about Leaflet's integration with Shiny? Well, it's really simple in a Shiny app to watch for the following events from a Leaflet map.
In my experience, this is fairly true. If there's truly advanced functionality required, then I recommend first referring to the R Leaflet library documentation, and if that doesn't meet the needs, explore the Leaflet js documentation.
Start by learning to manage packages and structure data for visualizations with the tidyverse and the pipe operator. Then there is an important question: Which library should you choose? The course introduces five popular options: Leaflet, Plotly, Highcharter, visNetwork, and DataTables (DT). Instructor Martin Hadley shows how to use these libraries to create scattergeo, choropleth, and geolines maps; stacked bar charts, scatter charts, bubble charts, and heat maps; treemaps and time series charts; interactive networks and graphs; and responsive, interactive data tables. Plus, learn how to customize your visualizations with legends and tooltips, and extract click information for Shiny apps.
- Installing htmlwidgets
- Selecting the best htmlwidget
- Creating interactive maps with Leaflet
- Creating interactive charts with Plotly and Highcharter
- Creating interactive networks with visNetwork
- Creating interactive tables with DT