How to Increase Legend Key Size in ggplot2

How to Increase Color Legend's Point Size in ggplot2
How to Increase Color Legend's Point Size in ggplot2

In this tutorial, we will learn how to increase the color legend’s point (or legend key) size in ggplot2. Often when you make a plot with multiple groups and large number of data points, the legend key for the color variable are small and can be not legible in the plot.

Here we will see examples of increasing the point size of the legend in a scatter plot using guides() function in ggplot2.

How to Increase Color Legend’s Point Size in ggplot2

Data preparation

Le us load the packages needed to make a scatter plot and increase the legend point size.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

We will be using palmer penguin datasets. The legibility of legend point is often an issue when you have more groups, therefor more colored legend keys or points in a scatter plot. To illustrate that we combine two categorical variables to create more groups. In the example below we combine species and sex variable using unite() function.

df <- penguins %>%
  drop_na() %>%
  unite(species_sex, c(species, sex))

The modified penguin data with new combined column looks like this

df %>% head()

## # A tibble: 6 × 7
##   species_sex   island bill_length_mm bill_depth_mm flipper_length_… body_mass_g
##   <chr>         <fct>           <dbl>         <dbl>            <int>       <int>
## 1 Adelie_male   Torge…           39.1          18.7              181        3750
## 2 Adelie_female Torge…           39.5          17.4              186        3800
## 3 Adelie_female Torge…           40.3          18                195        3250
## 4 Adelie_female Torge…           36.7          19.3              193        3450
## 5 Adelie_male   Torge…           39.3          20.6              190        3650
## 6 Adelie_female Torge…           38.9          17.8              181        3625
## # … with 1 more variable: year <int>

Scatter plot with multiple groups colored by legends

When we add color to a plot by variable, ggplot2 helps us making a legend to understand. An important part the legend is the legend key. In a scatter plot, the legend key is the colored circles or points. And by default it will match the size of the plot.

df %>%
  ggplot(aes(flipper_length_mm, body_mass_g, color=species_sex))+
  geom_point()+
  scale_color_brewer(palette="Dark2")
ggsave("ggplot2_scatter_plot_with_color_legend.png")

Here is an example scatter plot with multiple colors and legends.

Increase Legend Point Size in ggplot2


Increase the point size of legend with guides()

We can uses guides() function, we can customize various aspects of legends. In this example, we would like to increase the size of the legend key or the circle/point in the legend to make the colors easy to match with the plot.

df %>%
  ggplot(aes(flipper_length_mm, body_mass_g, color=species_sex))+
  geom_point()+
  scale_color_brewer(palette="Dark2")+
  guides(colour = guide_legend(override.aes = list(size=6)))
ggsave("ggplot2_scatter_plot_increase_color_legend_point_size.png")

We have used guide_legend() argument to guides function and increased the size by using override.aes argument. And now the legend keys are easy to see.

Increasing Color Legend size with guides()

Scatter plot of multiple groups with transparency

When you have a lot of data points overeplotting can be an issue and a solution to avoid the overplotting is to increase the transparency of data points in a scatter plot. In ggplot2 we can increase the transparency by using alpha argument and this helps to see overlapping data more clearly.

However, a challenge with this approach is that, setting tranparency using alpha also makes the legend keys or points more transparent and thus more difficult to read the plot.

Here is an example of using alpha to increase the transparency of points on plot also makes the legend key harder to map.

df %>%
  ggplot(aes(flipper_length_mm, body_mass_g, color=species_sex))+
  geom_point(alpha=0.5)+
  scale_color_brewer(palette="Dark2")
ggsave("ggplot2_scatter_plot_with_color_legend_with_alpha.png")
Scatter plot with color legend with alpha/transparency

Increase the legend key or point size of legend with guides() Second Example

Increasing the legend key size can alleviate the problem caused by alpha. Note we have increased the size of legend key, but not the legend key label.

df %>%
  ggplot(aes(flipper_length_mm, body_mass_g, color=species_sex))+
  geom_point(alpha=0.5)+
  scale_color_brewer(palette="Dark2")+
  guides(colour = guide_legend(override.aes = list(size=6)))
ggsave("scatter_plot_increase_legend_point_size-guide_legend.png")
Increase legend point size using guide_legend() in ggplot2
Exit mobile version