How To Rotate x-axis Text Labels in ggplot2

How to rotate x-axis text labels 45 degree?
How to rotate x-axis text labels 45 degree?

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.

Overlapping X-axis Text Labels in ggplot2

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..

How to rotate x-axis text labels 90 degree?

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))
How to rotate x-axis text labels 45 degree?

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.

How to rotate x-axis text labels 45 degree?

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.

Exit mobile version