Align axis label on the right with ggplot2 in R
Last Updated :
23 Jul, 2025
When creating visualizations in R using ggplot2, you might want to adjust the position of axis labels for improved readability or to meet specific formatting requirements. By default, ggplot2 position the y-axis label in the center of the axis, but you can customize this to align it to the right. This article will guide you through aligning axis labels on the right side of your ggplot2 plot in R Programming Language.
Prerequisites
Ensure that the ggplot2 the package is installed and loaded:
install.packages("ggplot2")
library(ggplot2)Creating a Basic ggplot2 Plot
Let's start by creating a basic plot using the mtcars dataset:
R
# Load the dataset
data(mtcars)
# Create a basic scatter plot
basic_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue") +
labs(x = "Weight (1000 lbs)", y = "Miles per Gallon (mpg)") +
theme_minimal()
# Display the plot
basic_plot
Output:
Creating a Basic ggplot2 PlotThis plot displays wt (weight) on the x-axis and mpg (miles per gallon) on the y-axis, with labels positioned in their default center alignment.
Step 1: Adjusting the y-axis Label Alignment
To align the y-axis label on the right, you need to customize the plot's theme using theme() and modify the axis.title.y property. The hjust parameter controls the horizontal alignment:
R
# Adjust y-axis label alignment
aligned_y_label <- basic_plot +
theme(axis.title.y = element_text(hjust = 1, angle = 0))
# Display the plot
aligned_y_label
Output:
Adjusting the y-axis Label Alignmenttheme(axis.title.y = element_text(hjust = 1, angle = 0)): This line modifies the y-axis label alignment.hjust = 1: Aligns the label to the right.angle = 0: Sets the angle of the y-axis label to 0, making it horizontal.
Step 2: Aligning the x-axis Label on the Right
If you want to align the x-axis label to the right, you can adjust the axis.title.x property:
R
# Adjust x-axis label alignment
aligned_x_label <- basic_plot +
theme(axis.title.x = element_text(hjust = 1))
# Display the plot
aligned_x_label
Output:
Aligning the x-axis Label on the RightStep 3: Aligning Both Axis Labels Together
You can align both the x-axis and y-axis labels on the right simultaneously:
R
# Align both axis labels on the right
aligned_both_labels <- basic_plot +
theme(axis.title.y = element_text(hjust = 1, angle = 0),
axis.title.x = element_text(hjust = 1))
# Display the plot
aligned_both_labels
Output:
Aligning Both Axis Labels TogetherCustomizing the Axis Label Appearance
You can further customize the appearance of the axis labels by changing their size, color, font, and more:
R
# Customize axis label appearance
customized_labels <- basic_plot +
theme(axis.title.y = element_text(hjust = 1, angle = 0, color = "red", size = 14, face = "bold"),
axis.title.x = element_text(hjust = 1, color = "blue", size = 14, face = "italic"))
# Display the plot
customized_labels
Output:
Align axis label on the right with ggplot2 in Rcolor = "red" and color = "blue" change the label colors.size = 14 adjusts the text size.face = "bold" and face = "italic" set the font style.
Conclusion
This article explain how to align the axis labels on the right using ggplot2 in R. Here’s a summary of what we covered:
- How to adjust the y-axis label alignment using
theme(axis.title.y). - How to adjust the x-axis label alignment using
theme(axis.title.x). - How to customize the appearance of axis labels, including color, size, and font.
- How to apply these techniques to different types of plots, such as scatter plots and bar plots.
These techniques provide a way to create cleaner, more readable plots tailored to your data visualization needs.
Explore
Introduction
Fundamentals of R
Variables
Input/Output
Control Flow
Functions
Data Structures
Object Oriented Programming
Error Handling
File Handling