Rotating and spacing axis labels in ggplot2 in R
Last Updated :
15 Nov, 2021
In this article, we will discuss how to Rotate and space axis labels in the ggplot2 in the R Programming Language.
Spacing the axis labels:
We can increase or decrease the space between the axis label and axis using the theme function. The axis.txt.x / axis.text.y parameter of theme() function is used to adjust the spacing using hjust and vjust argument of the element_text() function.
Syntax:
plot + theme( axis.text.x / axis.text.y = element_text( hjust, vjust )
where,
- hjust: determines the horizontal justification
- vjust: determines the vertical justification
Example:
In this example, we have added vertical space of 10 points using vjust command of theme function in the ggplot2 plot in the R Language.
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars
# of barplot
geom_bar(stat = "identity", fill="white")+
# vjust is used to justify the label vertically
theme(axis.text.x = element_text(vjust=-10))
Output:

Rotating Axis Labels
We can rotate the axis label and axis using the theme function. The axis.txt.x / axis.text.y parameter of theme() function is used to adjust the rotation of labels using the angle argument of the element_text() function.
Syntax:
plot + theme( axis.text.x / axis.text.y = element_text( angle )
where,
angle: determines the angle of rotation
Example:
In this example, we have made the rotation angle 90 degrees using the angle command of the theme function in the ggplot2 plot in the R Language. This makes the axis labels vertical.
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars
# of barplot
geom_bar(stat = "identity", fill="white")+
# rotate axis label using axis.text.x parameter
# of theme() 90 degree rotation makes label
# vertical
theme(axis.text.x = element_text(angle = 90))
Output:
Example:
In this example, we have rotated the axis label by 45 degrees that made it overlapping with the plot. so we moved it down using the vjust parameter to avoid overlapping.
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars
# of barplot
geom_bar(stat = "identity", fill="white")+
# rotate axis label using axis.text.x parameter of theme()
# vjust is used to justify the label to avoid
# overlapping with plot
theme(axis.text.x = element_text(angle = 45, vjust=0.5))
Output:
Similar Reads
Superscript and subscript axis labels in ggplot2 in R In this article, we will see how to use Superscript and Subscript axis labels in ggplot2 in R Programming Language. First we should load ggplot2 package using library() function. To install and load the ggplot2 package, write following command to R Console. # To Install ggplot2 package # (Write thi
3 min read
Remove Axis Labels and Ticks in ggplot2 Plot in R In this article, we will discuss how to remove axis labels and ticks in ggplot2 in R Programming Language. The axes labels and ticks can be removed in ggplot using the theme() method. This method is basically used to modify the non-data components of the made plot. It gives the plot a good graphical
2 min read
Remove Axis Labels using ggplot2 in R In this article, we are going to see how to remove axis labels of the ggplot2 plot in the R programming language. We will use theme() function from ggplot2 package. In this approach to remove the ggplot2 plot labels, the user first has to import and load the ggplot2 package in the R console, which i
2 min read
Rotating x-axis labels and changing theme in ggplot2 When visualizing data in R using ggplot2, you often need to adjust the appearance of your plots to make them clearer and more visually appealing. Two common adjustments include rotating x-axis labels for better readability and changing the overall theme of the plot to suit your presentation style or
4 min read
Modify axis, legend, and plot labels using ggplot2 in R In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters :Â Â stat : Set the stat parameter to
5 min read