Skip to content
R for the Rest of Us Logo

Make a Choropleth Map of Discrete Variables with ggplot2 (02_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 up the 02_06 project.

Use the your-turn.R script in 02_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

In this video there was quite a lot of information about how to work with factors using the {forcats} package.

While factors are part of base R it’s fairly painful to manipulate the levels of factors without the {forcats} package. The factors chapter of R for Data Science gives a great overview of both factors and {forcats}.

The paletteer package is an excellent resource for discovering useful color palettes. However, it’s very important to consider if your maps will make sense to readers with color blindness. Thankfully, the {colorblindr} package simplifies this with the following code:

remotes::install_github("wilkelab/cowplot")
colorblindr::cvd_grid(your_gg_map)

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

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

Kristina Bratkova

Kristina Bratkova

March 5, 2022

Hello,

thanks for this clear video. I was wondering if lines 37-38 are redundant if scale_fill_manual is used? (scale_fill_manual is set by the ordered factor given in the list of colours (which was set by base R syntax to be in order of descending area of the region))

Charlie Hadley

Charlie Hadley

March 7, 2022

Hi Kristina! This is a great question, and an absolutely correct observation. I made a quick gist to show that you're right, commenting out the fct_relevel() line isn't necessary to fix legend orders when using scale_fill_manual().

It's still a good idea to set the levels of your factors as they might have consequences in other aesthetics of your chart. Here's a slightly tautological example to demonstrate this. Let me know if you have any follow up questions. Cheers, Charlotte

Kristina Bratkova

Kristina Bratkova

March 10, 2022

Hello Charlotte,

many thanks for your answer, very interesting example!

Oscar Tetteh

Oscar Tetteh

April 7, 2023

Hello Charlotte,

I am trying to relate this to my current research. I have a shapefile and data sets. The aim is to map the data set to the shapefile. could you please help me with the codes or a recommendation?

Thank you

David Keyes

David Keyes

April 9, 2023

Hi Oscar, we unfortunately can't provide this type of personal support. If you have any questions about running code in the lessons, please let us know!