How to Rotate and Space Axis Labels in ggplot2 with R

by | Programming, R, Tips

You can rotate the axis labels by using angle parameter of the element_text() function when modifying the theme of your plot, for example:

theme(axis.text.x = element_text(angle = 90, vjust = 0.5)

We can use vjust and hjust in element_text() to add horizontal and vertical space to the axis labels.

This tutorial will go through how to rotate and space axis labels using ggplot2 in R with code examples.


Example

Let’s look at an example of creating a bar chart using the gapminder dataset. First we will install and load ggplot2 and dplyr.

install.packages("ggplot2")
install.packages("dplyr")
library(ggplot2)
library(dplyr)
library(gapminder)

Next, we will use the pipe operator to get the mean life expectancy of ten randomly sampled countries.

df <- gapminder %>%
group_by(country) %>%
summarize(mean_life = mean(lifeExp)) %>%
sample_n(10) %>%
mutate(country=fct_reorder(country, mean_life))

We can plot the resultant data frame as a bar chart as follows:

ggplot(df, aes(x=country, y=mean_life)) + 
 geom_bar(stat="identity")
Mean life vs Country with axis tick labels
Mean life vs Country with axis tick labels

However, we can see that some of the x-axis tick labels are overlapping making them hard to read.

Solution: element_text

We can rotate the x-axis tick labels to make them easier to read. We can pass the angle to rotate the text by to the angle parameter in the element_text() function. We can also adjust the spacing using hjust and vjust, where hjust determines the horizontal justification and vjust determines the vertical justification.

Let’s look at the revised code:

df <- gapminder %>%
group_by(country) %>%
summarize(mean_life = mean(lifeExp)) %>%
sample_n(10) %>%
mutate(country=fct_reorder(country, mean_life))
ggplot(df, aes(x=country, y=mean_life)) + 

geom_bar(stat="identity") +    

theme(axis.text.x = element_text(angle = 90, vjust = 0.5)

We also added vjust = 0.5 so that the x-axis tick label aligns with the tick.

Mean life vs Country with axis tick labels rotated
Mean life vs Country with axis tick labels rotated

Solution: Coord Flip

We can also use the coord_flip() function to flip the axes. We need to put the coord_flip() after the geom_bar() constructor as follows:

ggplot(df, aes(x=country, y=mean_life)) + 

geom_bar(stat="identity") +    

coord_flip()
Mean life vs Country with coord_flip
Mean life vs Country with coord_flip

Solution: str_wrap

If we want to keep the x-axis tick labels horizontal but stop the text from overlapping we can use str_wrap(), which wraps text to a specific number of characters per line. The function is typically used to create paragraphs. We can create a new column in the data frame with the text wrapped to 8 characters per line.

library(stringr)

df$country2 <- str_wrap(df$country, width=8)

ggplot(df, aes(x=country2, y=mean_life)) + 

geom_bar(stat="identity")    

Note that when we create the ggplot() object we pass the new column as the x-variable in the aesthetic mappings aes. Let’s run the code to see the result:

Mean life vs Country with str_wrap applied to axis tick label
Mean life vs Country with str_wrap applied to axis tick label

We can see the that x-axis tick labels are horizontal and are neatly separated.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on plotting with R, go to the article:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!