• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Increase Legend Key Size in ggplot2

datavizpyr · January 16, 2023 ·

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
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 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()
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
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")
How to increase legend point size in ggplot2 R
Increase legend point size using guide_legend() in ggplot2

Related posts:

How to add colors to axis tick labels using ggtext?How to Add Colors to Axis Tick Label in ggplot2 Customising Histogram plotting functionHow to write a function make histograms with ggplot Customizing lollipop. plot with geom_lollipop() in ggaltLollipop plot with ggplot extension ggalt How to reverse legend key order in ggplot2How to reverse legend key order in ggplot2

Filed Under: ggplot2, R, Uncategorized Tagged With: ggplot2 guide_legend(), ggplot2 guides(), ggplot2 increase legend point size

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version