One of the common problems while making data visualization is making the axis label clearly legible. Often they tend to overlap and make it difficult to read the text labels. There are a few ways we can make the axis text label easy to read.
In this tutorial, we will learn how to rotate axis text labels so that it is easy to read axis text labels. We will use TidyTuesday dataset on world-wide crop yield.
Let us load tidyverse, the suite of R packages and set a ggplot2 theme for our plots.
library(tidyverse) theme_set(theme_bw(16))
Boxplot example with overlapping x-axis label text
Let us load the file containing global crop yield across multiple years directly from TidyTuesday github page.
key_crop_yields <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/key_crop_yields.csv')
Let us get a quick peek at the data.
key_crop_yields %>% head() ## # A tibble: 6 x 14 ## Entity Code Year `Wheat (tonnes … `Rice (tonnes p… `Maize (tonnes … ## <chr> <chr> <dbl> <dbl> <dbl> <dbl> ## 1 Afgha… AFG 1961 1.02 1.52 1.4 ## 2 Afgha… AFG 1962 0.974 1.52 1.4 ## 3 Afgha… AFG 1963 0.832 1.52 1.43 ## 4 Afgha… AFG 1964 0.951 1.73 1.43 ## 5 Afgha… AFG 1965 0.972 1.73 1.44 ## 6 Afgha… AFG 1966 0.867 1.52 1.44 ## # … with 8 more variables: `Soybeans (tonnes per hectare)` <dbl>, `Potatoes ## # (tonnes per hectare)` <dbl>, `Beans (tonnes per hectare)` <dbl>, `Peas ## # (tonnes per hectare)` <dbl>, `Cassava (tonnes per hectare)` <dbl>, `Barley ## # (tonnes per hectare)` <dbl>, `Cocoa beans (tonnes per hectare)` <dbl>, ## # `Bananas (tonnes per hectare)` <dbl>
We will be making a boxplot of rice yield for selected countries to show how to rotate x-axis text labels
countries <- c("China", "Taiwan", "Philippines", "India","Bangladesh", "Malaysia", "Mexico", "United States")
Let us make a boxplot using geom_boxplot() and with jittrered data points using geom_jitter(). In the boxplot, we have the selected countries are on x-axis and rice yield in tonnes per hectare on y-axis.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)
We can see that some of the countries overlap on x-axis.
How To Rotate x-axis Text Label to using guides()
With ggplot2 3.5.0 we have a new way to rotate x-axis text labels to angle we want using guide_axis() function. In the example below we use guides() function as additional layer with guide_axis() as its argument. In the example below we rotate the labels by 90 degree angle, but that could customized to other angle interest.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)+ guides(x = guide_axis(angle = 90))+ labs(title="Rotate axis label: guides() & guide_axis()") ggsave("rotating_x_axis_text_label_using_guides_guide_axis_ggplot2.png")
How To Rotate x-axis Text Label to using scale_x_discrete() & guide_axis()
Another variant of using guide axis() function to rotate x-axis text labels is to use with scale_x_discrete() function instead of guides(). In the example below we rotate the labels by 90 degree angle, but we can change that.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)+ scale_x_discrete(guide= guide_axis(angle = 90))+ labs(title="Rotate axis label: scale_x_discrete() & guide_axis()") ggsave("rotating_x_axis_text_label_using_scale_x_discrete_guide_axis_ggplot2.png")
How To Rotate x-axis Text Label to 90 Degrees
To make the x-axis text label easy to read, let us rotate the labels by 90 degrees. We can rotate axis text labels using theme() function in ggplot2. To rotate x-axis text labels, we use “axis.text.x” as argument to theme() function. And we specify “element_text(angle = 90)” to rotate the x-axis text by an angle 90 degree.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)+ theme(axis.text.x = element_text(angle = 90))
We have successfully rotated x-axis text labels to 90 degrees and thus avoided overlapping x-axis labels..
Rotating x-axis Text Label by 45 Degrees
Notice that rotating x-axis text labels to 90 degree takes extra space in the plot. One way to make it better is to rotate x-axis label to 45 degree instead of 90 degrees.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)+ theme(axis.text.x = element_text(angle = 45))
Adjusting the Rotated x-axis Text Label using “hjust”
Rotating x-axis text labels to 45 degrees makes the label overlap with the plot and we can avoid this by adjusting the text location using hjust argument to theme’s text element with element_text(). We use axis.text.x as we want to change the look of x-axis text.
key_crop_yields %>% filter(Entity %in% countries) %>% ggplot(aes(Entity,`Rice (tonnes per hectare)`)) + geom_boxplot() + geom_jitter(width=0.15)+ theme(axis.text.x = element_text(angle = 45, hjust=1))
Now we have successfully rotated x-axis text labels to make the labels legible.
Note that rotating axis text labels are not always the best solution. In most cases we can use coord_flip() to switch and and y-axis and make the axis text easy to read. And with ggplot2 version 3.3.0, we can avoid overlapping label texts by moving the labels along y-axis alternatively.