• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggplot2 3.3.0. Is Here : Two New Features You Must Know

ggplot2 3.3.0. Is Here : Two New Features You Must Know

March 6, 2020 by cmdlinetips

What is New in ggplot2 v3_3_0?
ggplot2_v3_3_0
ggplot2, the R package that lets you create graphics using the Grammar of Graphics has a new version. The new version of ggplot2; version 3.3.0 has lots of changes and it available on CRAN. Introducing ggplot2 v 3.3.0 Thomas Lin Pedersen says that the new version “is packed with features, big and small” and a lot of internal rewrites of the ggplot2 code base.

There are tonnes of new features to learn in the new version of ggplot. In this post, we will focus on
two new features you will love and solves a common problem in making plots with ggplot2.

Let us install ggplot2 from CRAN.

# install ggplot2
install.packages("ggplot2")

And verify we have the latest ggplot2 version installed using packageVersion() function.

library(tidyverse)
packageVersion("ggplot2")

3.3.0

The Problem With Overlapping Axis Texts

Making plots with ggplot2 is fantastic. But one of the annoying pain points of making a plot with ggplot2 is “overlapping text on axis”. Basically, your x-axis text on ticks are long and overlapp with each other and make it impossible to read them.

For example, let use try to make a simple barplot with a lot of variables on x-axis.

ggplot(mpg) +
  geom_bar(aes(x = manufacturer)) 

If you look at the x-axis text in the barplot, they all overlap.

Overlapping Axis Text in ggplot2
Overlapping Axis Text in ggplot2

Typically one has to resort to a trick or two to avoid the overlapping axis text issue.

For example, you might think that the x-axis text overlaps because the figure size is small. So, you might increase the width of the plot to accommodate the axis text.

ggplot(mpg) +
  geom_bar(aes(x = manufacturer)) 
# increase the width and save the plot 
ggsave("ggplot_with_overlapping_axis_text_wide.png", width=12)

Yes, this might work for you sometimes. However, this is too wide and may not work in general.

Avoid overlapping axis text with wide ggplot.
Avoid overlapping axis text with wide ggplot2.

Another common trick is to flip the two axes using coord_flip(), so that the overlapping text on x-axis will be on y-axis now.

ggplot(mpg) +
  geom_bar(aes(x = manufacturer)) + 
  coord_flip()
ggsave("ggplot_horizontal_plot_with_coord_flip.png")

Using coord_flip() is really an elegant solution and makes the axis text easily readable. Since x-axis text will now be on y-axis and on separate lines, you won’t have overlapping axis text problem.

Horizontal Barplot with coord_flip() in ggplot2
Horizontal Barplot with coord_flip() in ggplot2

However, you might wonder why should you add coord_flip() why not just swap x and y variables.

Dodge Overlapping Axis Text with guide_axis() in ggplot2 version 3.3.0

However, with ggplot2 version 3.3.0, You don’t have to wonder now and you don’t have to worry about overlapping axis texts as well.

Let us see examples of what you can do now to these “pesky” overlapping texts now. The new version of ggplot2 contains total rewrite of code for axis. Explaining what is new in the new code for axis, Thomas Lin Pedersen says

While, at a high level, axes and legends are equivalent (they are both guides used for scales), this has not been true for the underlying code. With this release, we have streamlined the implementation considerably, and paved the way for a full guide rewrite in a future release (guides are one of the last part waiting to be updated to ggproto). Apart from making our life as ggplot2 developers easier, the rewrite also comes with a slew of user-facing improvements and features. All of this is contained in the new guide_axis() function that works equivalently to e.g. guide_legend()

With guide_axis(), we can add dodge to our axis label texts to avoid overlapping texts. In the code below, we have used guide_axis() function with n.dodge=2 inside scale_x_discrete() to dodge overlapping text on x-axis.

ggplot(mpg) +
  geom_bar(aes(x = manufacturer)) + 
  scale_x_discrete(guide = guide_axis(n.dodge = 2))+
  labs(subtitle="Avoid Overlapping Axis Text with guide_axis() in ggplot2 3.3.0")+
  theme(axis.text.x = element_text(size = 11))
ggsave("ggplot_guide_axis_dodge_to_avoid_overlapping_axis_text.png",)

You can see that guide_axis() nicely avoided the text overlap by placing them with dodge.

Avoid Overlapping Text by Dodging with guide_axis() in ggplot2 v3.3.0
Avoid Overlapping Text by dodging with guide_axis() in ggplot2 v3.3.0

Increasing the n.dodge value will place axis text further away from x-axis as shown below for n.dodge = 4.

Avoid Overlapping Label Text with guide_axis()
Avoid Overlapping Label Text with guide_axis(n.dodge = 4)

Skip Some of Overlapping Axis Text with guide_axis() in ggplot2 version 3.3.0

In some cases, we can not show the text for all the labels on x-axis. In those scenarios, completely skipping some of the axis text would be great. We can use guide_axis() to drop overlapping axis text by specifying the argument “check.overlap = TRUE”.

ggplot(mpg) +
  geom_bar(aes(x = manufacturer)) + 
  theme(axis.text.x = element_text(size = 11))+
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE))
ggsave("ggplot_guide_axis_dodge_n_skip_to_avoid_overlapping_axis_text.png",)

Now we have a barplot with overlapping axis text removed from the plot. This option will keep the first and last labels dropping some of the intermediate overlapping labels on x-axis.

Avoid Overlapping Axis Text by Skipping with guide_axis in ggplot2 3.3.0
Avoid Overlapping Axis Text by Skipping with guide_axis in ggplot2 3.3.0

Horizontal plots without coord_flip() using Bi-directional geoms

Another new feature to love is the ability to change direction of ggplot. In the new version of ggpot2, the team has updated “all the directional stats and geoms to work in both directions”. What this means is that we can easily make a horizontal ggplot without using coord_flip().

For example, to make horizontal barplot as shown above, we can simply specify our y-axis instead of x-axis.

ggplot(mpg) + 
  geom_bar(aes(y = manufacturer))
ggsave("horizontal_barplot_with_ggplot2_v2_3_0_without_coord_flip.png")

Yes, without using coord_flip() you get horizontal barplot as you wanted.

ggplot2 3.3.0: Horizontal Barplots with out coord_flip()
ggplot2 3.3.0: Horizontal Barplots with out coord_flip()

Obviously, the bidirectionality works for other types of plots too. For example, if we want to make horizontal boxplot using gapminder data with continent on y-axis and lifeExp on x-axis, we simply specify that inside aesthetics.

library(gapminder)
gapminder %>%
  ggplot(aes(x = lifeExp, y=continent, fill=continent))+
  geom_boxplot()+
  theme(legend.position="none")
ggsave("horizontal_boxplot_with_ggplot2_v2_3_0_without_coord_flip.png")

Voila, we would get a nice horizontal box plot without using coord_flip().

ggplot2 3.3.0: Horizontal Boxplots with out coord_flip()
ggplot2 3.3.0: Horizontal Boxplots with out coord_flip()

With such a long introduction to these two new features, you could clearly see the love of these two features. However, there are a lot more new features in ggplot2 to check out. Read the blogpost announcing the new version of ggplot2 3.3.0 to learn more . Looking forward to learn and use them.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail8 tips to make better barplots with ggplot2 in R Faceting and reordering: reorder_within() tidytextFaceting and Reordering with ggplot2 How to annotate with ellipses in ggplot2 using ggforce?Getting started with ggforce – a ggplot2 extension package How To Highlight Select Data Points with ggplot2 in R?

Filed Under: ggplot2, ggplot2 version 3.3.0, R Tagged With: ggplot2, gplot2 version 3.3.0, R

Reader Interactions

Trackbacks

  1. How To Dodge Overlapping Text on X-axis Labels in ggplot2? - Data Viz with Python and R says:
    March 11, 2020 at 5:45 am

    […] with the new version of ggplot2 2.3.0, one can easily dodge overlapping text on x-axis. ggplot2 version 2.3.0 has a function guide_axis() […]

  2. How to Make Horizontal Boxplot with ggplot2 version 3.3.0? - Data Viz with Python and R says:
    April 1, 2020 at 6:26 am

    […] till now the only way to make a plot horizontal is to use flip the axis using coord_flip(). With ggplot2 version 3.3.0, we can easily flip axis and make horizontal boxplot or horizontal barplot without using […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version