Day 13 of viz with me

Data viz Beginner ggplot legends theme() DataViz Challenge

Today we will move the legends around.

Soundarya Soundararajan true
2024-10-13

Yesterday, we learned how to adjust the axis limits in a scatter plot.

Welcome to Day 13 of “Viz with Me”!

Goals for Today

Today, we will learn how to move the legends in a ggplot graph to different positions.

What are Legends?

In data visualization, legends represent the variables and their levels, which are often associated with color, size, or other aesthetic mappings in the plot.

Let’s start with an example:

library(palmerpenguins)
library(tidyverse)

penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g, color = species)) +
  geom_point() +
  labs(title = "Bill Length vs Body Mass",
      caption = "Data Source: Palmerpenguins",
      x="Bill Length",
      y="Body mass of penguins") +
    theme_minimal()

Notice that the color variable (species) is automatically added as a legend on the right side of the plot. But what if we want to move it? For example, I personally think legends often look better at the top of the plot.

Here’s how we can do that:

penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g, color = species)) +
  geom_point() +
 labs(title = "Bill Length vs Body Mass",
      caption = "Data Source: Palmerpenguins",
      x="Bill Length",
      y="Body mass of penguins") +
    theme_minimal()+
  theme(legend.position = "top")  # Moves the legend to the top

A note on positioning

Now, if you tried this and ended up with the same plot as before, don’t worry! You might have missed the positioning of of the theme(legend.position = "top"). If you are applying other themes, such as theme_minimal(), the order in which you apply them matters. Always apply legend.position after the theme function.

For example, if you apply legend.position before theme_minimal(), the theme will override your legend position, and you’ll end up with the default layout. Here’s an example:

penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g, color = species)) +
  geom_point() +
   labs(title = "Bill Length vs Body Mass",
      caption = "Data Source: Palmerpenguins",
      x="Bill Length",
      y="Body mass of penguins") +
  theme(legend.position = "top") + # This will be overridden
  theme_minimal() 

The theme_minimal() will override the legend position, and you’ll get the default right-side legend placement.

So, always remember this when adding customizations—if you’re changing elements like the legend position, make sure those customizations are added after any general themes (like theme_minimal()). This will ensure your changes aren’t overridden and your plot looks exactly as you intend.

And that’s it! You can also move the legend to "bottom", “left", or "right" by changing the argument in legend.position.

Happy plotting, and see you tomorrow!

Citation

For attribution, please cite this work as

Soundararajan (2024, Oct. 13). My R Space: Day 13 of viz with me. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-10-13-day-13-of-viz-with-me/

BibTeX citation

@misc{soundararajan2024day,
  author = {Soundararajan, Soundarya},
  title = {My R Space: Day 13 of viz with me},
  url = {https://github.com/soundarya24/SoundBlog/posts/2024-10-13-day-13-of-viz-with-me/},
  year = {2024}
}