How to Rotate Axis Labels in R

Sheeraz Gul Feb 02, 2024
  1. Rotate Axis Labels in Base R
  2. Rotate Axis Labels Horizontally
  3. Rotate Axis Labels Vertically
  4. Rotate Axis Labels Perpendicular to the Axis
  5. Rotate Axis Labels in ggplot
  6. Rotate Axis Labels to 90 Degree in ggplot
  7. Rotate Axis Labels to 45 Degree in ggplot
How to Rotate Axis Labels in R

The Base R and ggplot have different ways to rotate axis labels in R. This tutorial demonstrates how to rotate axis labels in R.

Rotate Axis Labels in Base R

In base R, we can rotate the axis label horizontally, vertically, or perpendicular to the axis. Let’s first show the plot for which we will rotate labels, and after that, each method is demonstrated in the following.

Example Code:

# Create example Data
set.seed(99999)

xLabel <- rnorm(1000)
yLabel <- rnorm(1000)

# The Default Plot
plot(xLabel, yLabel)

Output:

Default Plot

Rotate Axis Labels Horizontally

We can rotate the axis label horizontally by passing the las=1 in the plot.

Example Code:

# Create example Data
set.seed(99999)

xLabel <- rnorm(1000)
yLabel <- rnorm(1000)

# The Horizontal Axis Plot
plot(xLabel, yLabel, las=1)

The code above creates a plot with horizontal axis labels.

Output:

Horizontal Plot

Rotate Axis Labels Vertically

We can rotate the axis label vertically by passing the las=3 in the plot.

Example Code:

# Create example Data
set.seed(99999)

xLabel <- rnorm(1000)
yLabel <- rnorm(1000)

# The Vertical Axis Plot
plot(xLabel, yLabel, las=3)

The code above creates a plot with vertical axis labels.

Output:

Vertical Plot

Rotate Axis Labels Perpendicular to the Axis

We can rotate the axis label perpendicular to the axis by passing the las=2 in the plot.

Example Code:

# Create example Data
set.seed(99999)

xLabel <- rnorm(1000)
yLabel <- rnorm(1000)

# The Perpendicular Axis Plot
plot(xLabel, yLabel, las=2)

The code above creates a plot perpendicular to axis labels.

Output:

Perpendicular Axis Plot

The las value can be changed in any type of plot in Base R.

Rotate Axis Labels in ggplot

Using the following syntax, we can rotate the axis labels in ggplot2.

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

The labels will rotate to a 45-degree angle, and vjust and hjust will control the vertical and horizontal justification of the label’s text. Let’s create a plot for which we can rotate the axis label in ggplot2.

Example Code:

# Create example Data
Delftstack <- data.frame(Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'),
                         Id=c(101, 102, 103, 104, 105))

#view the data
Delftstack

#plot the data using gglpot
library(ggplot2)

#create bar plot
ggplot(data=Delftstack, aes(x=Designation, y=Id)) +
  geom_bar(stat="identity")

The code above will create a default plot from the given data.

Output:

Default Ggplot2

Rotate Axis Labels to 90 Degree in ggplot

We can give the value 90 to the angle to rotate the axis labels to 90 degrees in ggplot2.

Example Code:

# Create example Data
Delftstack <- data.frame(Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'),
                         Id=c(101, 102, 103, 104, 105))

#plot the data using gglpot2
library(ggplot2)

#create bar plot
ggplot(data=Delftstack, aes(x=Designation, y=Id)) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(angle=90, vjust=.5, hjust=1))

The code above will create a gglpot2 with an axis rotated to 90 degrees.

Output:

90 Degree Ggplot

Rotate Axis Labels to 45 Degree in ggplot

We can give the value 45 to the angle to rotate the axis labels to 45 degrees in ggplot2.

Example Code:

# Create example Data
Delftstack <- data.frame(Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'),
                         Id=c(101, 102, 103, 104, 105))

#plot the data using gglpot2
library(ggplot2)

#create bar plot
ggplot(data=Delftstack, aes(x=Designation, y=Id)) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1))

The code above will create a gglpot with an axis rotated to 45 degrees.

Output:

45 Degree Ggplot

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook