Skip to content
R for the Rest of Us Logo

Make a Choropleth Map of Discrete Variables with leaflet (03_06)

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

Open project 03_06. Use the your-turn.R script in 03_06 to make this map.

  • Use the custom colors.

  • Ensure the guide is ordered by size of region.

  • Add a title to the guide.

Learn More

It is much easier to work with NA values in {leaflet} maps than with {gglot2}!

In this video I used a slightly different approach to working with factors than in the {ggplot2} video about discrete choropleth. As before, if you want to learn more about factors then I recommend the factors chapter of R for Data Science.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Hi there Charlie; in the "my turn" example, I was expecting that Hulu would end up green, ESPN red, Netflix black ,and Amazon Prime blue. In reality, it looks like Hulu ended up black, ESPN green, Netflix red, and Amazon Prime blue (last one is in line with expectations). I think what's in your video is the same result .. . does this have to do with the arranging in the fct_rev, perhaps linked to some aspect of the unlist function? Seems like a minor point, and it took me a while to realize what I was seeing - sorry if I missed something there, thanks!

Charlie Hadley

Charlie Hadley

March 1, 2023

Hello Jeremy,

Sorry for the delay in replying! Thanks for pointing out this issue - it had obviously escaped my eye too.

The issue comes from me not paying attention to the order in which code was run! I've put the fixed code in this gist https://gist.github.com/charliejhadley/81b836555b51fdb82a542f7f04afb06a

Jordan Trachtenberg

Jordan Trachtenberg

May 18, 2023

Hi Charlie, this question is beyond the scope of this video but I was hoping you could steer me in the direction of a helpful resource. Is there a way that you can create an interactive filter on your map to be able to select one or several of the categories? For instance, a button that would allow me to select only the Netflix datapoints? I'm guessing this requires Shiny and more extensive programming but I thought I would ask. The reason I'm asking is because I'm trying to make a map dashboard with various filter variables like company, date, and cost that I would like the user to be able to filter.

David Keyes

David Keyes

May 18, 2023

I'll let Charlie chime in with any additional ideas, but we did this on a project. You can see the map itself here and the code used to make the map here.

Jordan Trachtenberg

Jordan Trachtenberg

May 19, 2023

Thanks, David! This example is very helpful to see the possibilities. I probably won't be able to achieve this level of snazziness with my first project but it convinces me that I should sign up for the GitHub course soon and delve more into website development. I'm currently trying to just make the project into an html file but I know the client would like to be able to embed in their website if possible. This is a steep learning curve!

Charlie Hadley

Charlie Hadley

May 21, 2023

Hello Jordan!

The map that David shared may look complex but it makes use of functionality baked into {leaflet} that doesn't require {shiny}. This documentation page demonstrates how you can use addLayersControl() to add the kind of buttons you mentioned https://rstudio.github.io/leaflet/showhide.html

Cheers,

Charlie

Jordan Trachtenberg

Jordan Trachtenberg

May 26, 2023

Thank you for sharing this resource, Charlie! I'm going to dig into it.