Table of Contents
Removing axis labels in ggplot2 can be achieved by using the “labs” function and specifying “NULL” as the label for the desired axis. This will remove the label from the plot and leave the axis with no label. Additionally, the “theme” function can be used to modify the appearance of the axis, including removing the label and tick marks altogether. This can be useful for creating a clean and minimalist plot.
Remove Axis Labels in ggplot2 (With Examples)
You can use the following basic syntax to remove axis labels in ggplot2:
ggplot(df, aes(x=x, y=y))+ geom_point() + theme(axis.text.x=element_blank(), #remove x axis labels axis.ticks.x=element_blank(), #remove x axis ticks axis.text.y=element_blank(), #remove y axis labels axis.ticks.y=element_blank() #remove y axis ticks )
The following examples show how to use this syntax in practice.
Example 1: Remove X-Axis Labels
The following code shows how to remove x-axis labels from a scatterplot in ggplot2:
library(ggplot2)
#create data frame
df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
#create scatterplot
ggplot(df, aes(x=x, y=y))+
geom_point() +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())
Notice that only the x-axis labels (and tick marks) have been removed.
Example 2: Remove Y-Axis Labels
The following code shows how to remove y-axis labels from a scatterplot in ggplot2:
library(ggplot2)
#create data frame
df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
#create scatterplot
ggplot(df, aes(x=x, y=y))+
geom_point() +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
Notice that just the y-axis labels (and tick marks) have been removed.
Example 3: Remove Both Axis Labels
The following code shows how to remove labels from both axes in a scatterplot in ggplot2:
library(ggplot2)
#create data frame
df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
#create scatterplot
ggplot(df, aes(x=x, y=y))+
geom_point() +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
Additional Resources
The following tutorials explain how to perform other common functions in ggplot2: