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.
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.
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.
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.
Increasing the n.dodge value will place axis text further away from x-axis as shown below for 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.
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.
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().
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.
2 comments
Comments are closed.