Day 12 of viz with me

Data viz Beginner ggplot axis limits xlim ylim DataViz Challenge

Today we learn how to adjust axis limits.


Author

Affiliation

Soundarya Soundararajan

 

Published

Oct. 11, 2024

Citation

Soundararajan, 2024


Welcome to Day 12 of “Viz with Me”!

Goal for today: Learn how to change the axis limits in a scatter plot in R.

Here’s a scatter plot created using the Palmer Penguins dataset:

# Load the necessary libraries
library(tidyverse)      
library(palmerpenguins) 

# A scatter plot of bill length vs body mass
penguins %>% 
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +  
  geom_point()                                      

Take a look at the plot that this code generates. You’ll notice that the x-axis and y-axis automatically adjust based on the data. This default behavior sometimes doesn’t start the axes at zero.

Why Adjust Axis Limits?

When axes don’t start at zero, it can distort the perception of the data. For example, in a scatter plot, trends might look stronger or weaker than they actually are. In the context of data visualization ethics, it’s important to be mindful of this. Both when you create visualizations and when you interpret them, pay attention to whether axis distortions are present.

How to Set the X-Axis or Y-Axis to Start from Zero

You can easily set the limits of your axes using the xlim() and ylim() functions. Here’s how you can force the x-axis to start at zero:

# Adjust the x-axis to start at 0
penguins %>% 
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +  
  geom_point() +                                     
  xlim(0, NA)   # Set x-axis limit to start from 0

xlim(0, NA): The 0 ensures the x-axis starts at zero. The NA means there is no upper limit, so it will adjust based on the data.

You can use a similar approach for the y-axis with ylim(0, NA) if needed.

Extending the Limits Beyond Data for Annotation

What if you want to extend the plot on the right or upper side to annotate certain data points? You can specify the upper limit explicitly:

# Extend x-axis to a custom limit
penguins %>% 
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +  
  geom_point() +                                     
  xlim(0, 60)   # Set the x-axis to start at 0 and extend to 60

This will ensure that the plot has enough space on the right, useful for adding annotations or giving your data more breathing room.

This concept of axis distortion reminds us to be mindful when creating visualizations, especially when presenting data to others.

Take a moment to check the axes whenever you encounter a chart—whether it’s one you’ve generated or one that’s being presented to you!

I shall see you tomorrow with another interesting topic. Until then, happy visualizing!

Jump to tomorrow for moving the legend around.

Footnotes

    Citation

    For attribution, please cite this work as

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

    BibTeX citation

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