How can I rotate axis labels in ggplot2 and what are some examples of doing so? 2

How can I rotate axis labels in ggplot2 and what are some examples of doing so?

The process of rotating axis labels in ggplot2 involves adjusting the orientation of the labels on the x and y axes of a graph. This can be done by using the “theme” function and specifying the angle at which the labels should be rotated. Some examples of rotating axis labels in ggplot2 include rotating the x-axis labels by 45 degrees, rotating both x and y-axis labels by 90 degrees, and rotating the y-axis labels by 180 degrees. This technique can be useful for improving the readability and visual appeal of a graph, particularly when dealing with long or overlapping labels.

Rotate Axis Labels in ggplot2 (With Examples)


You can use the following syntax to rotate axis labels in a ggplot2 plot:

p + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

The angle controls the angle of the text while vjust and hjust control the vertical and horizontal justification of the text.

The following step-by-step example shows how to use this syntax in practice.

Step 1: Create the Data Frame

First, let’s create a simple data frame:

#create data frame
df = data.frame(team=c('The Amazing Amazon Anteaters',
                       'The Rowdy Racing Raccoons',
                       'The Crazy Camping Cobras'),
                points=c(14, 22, 11))

#view data frame
df

                          team points
1 The Amazing Amazon Anteaters     14
2    The Rowdy Racing Raccoons     22
3     The Crazy Camping Cobras     11

Step 2: Create a Bar Plot

Next, let’s create a bar plot to visualize the points scored by each team:

library(ggplot2)

#create bar plot
ggplot(data=df, aes(x=team, y=points)) +
  geom_bar(stat="identity")

Step 3: Rotate the Axis Labels of the Plot

We can use the following code to rotate the x-axis labels 90 degrees:

library(ggplot2)

#create bar plot with axis labels rotated 90 degrees
ggplot(data=df, aes(x=team, y=points)) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(angle=90, vjust=.5, hjust=1))

Or we can use the following code to rotate the x-axis labels 45 degrees:

library(ggplot2)

#create bar plot with axis labels rotated 90 degrees
ggplot(data=df, aes(x=team, y=points)) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1))

Depending on the angle you rotate the labels, you may need to adjust the vjust and hjust values to ensure that the labels are close enough to the plot.

The following tutorials explain how to perform other common tasks in ggplot2:

How to Reverse Order of Axis in ggplot2

Cite this article

stats writer (2024). How can I rotate axis labels in ggplot2 and what are some examples of doing so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rotate-axis-labels-in-ggplot2-and-what-are-some-examples-of-doing-so/

stats writer. "How can I rotate axis labels in ggplot2 and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-rotate-axis-labels-in-ggplot2-and-what-are-some-examples-of-doing-so/.

stats writer. "How can I rotate axis labels in ggplot2 and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rotate-axis-labels-in-ggplot2-and-what-are-some-examples-of-doing-so/.

stats writer (2024) 'How can I rotate axis labels in ggplot2 and what are some examples of doing so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rotate-axis-labels-in-ggplot2-and-what-are-some-examples-of-doing-so/.

[1] stats writer, "How can I rotate axis labels in ggplot2 and what are some examples of doing so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I rotate axis labels in ggplot2 and what are some examples of doing so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top