Positioning and formatting a legend using the ggplot2 R package (CC141)
Channel: Riffomonas Project
Date: 2021-08-30

In this Code Club, Pat goes over everything you'd want to know about positioning and formatting a legend using the ggplot2 R package. Sick of looking at the default legend in your R plots? This is the episode for you! Pat will use a variety of theme arguments to format a legend and show how to use the guides and guide_legend functions. He'll also spend time completing his effort to convert a version of a figure he made that was originally published by Ipsos into a more stylized one published by chartr. The data depict the percentage of people in 15 countries who would be willing to receive the COVID-19 vaccine as of August and October of 2020.

Pat uses functions from the tidyverse including functions from the #ggplot2, #dplyr, showtext, ggtext and glue packages in #RStudio. The accompanying blog post can be found at https://www.riffomonas.org/code_club/2021-08-30-legend.

Want more practice on the concepts covered in Code Club? You can sign up for my weekly newsletter at https://shop.riffomonas.org/youtube to get practice problems, tips, and insights.

R: https://r-project.org
RStudio: https://rstudio.com
Workshops: https://www.mothur.org/wiki/workshops

Here's a link for the chartr newsletter: https://www.chartr.co

If you're interested in taking an upcoming 3 day R workshop be sure to check out our schedule at https://riffomonas.org/workshops/


You can also find complete tutorials for learning R with the tidyverse using...
Microbial ecology data: https://www.riffomonas.org/minimalR/
General data: https://www.riffomonas.org/generalR/

0:00 Introduction
1:55 Setting the dimensions of the figure
3:02 Renaming countries with recode function
5:03 Removing "Total" data from figure
9:15 Showing and hiding legends with guides function
10:45 Positioning and orienting the legend
14:12 Formatting the legend
18:16 Overriding the aes values in a legend
19:41 Adjusting margins
21:35 Formatting figure to match chartr version
22:12 Recap