4.2 C
London
Friday, December 20, 2024
HomeTidyverse in Rggplot2 in RHow to Remove Axis Labels in ggplot2 (With Examples)

How to Remove Axis Labels in ggplot2 (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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() 
        )

remove x-axis labels in ggplot2

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 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() 
        )

remove y-axis labels in ggplot2

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 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() 
        )

ggplot2 scatterplot with no axis labels

The labels and tick marks on both axes have been removed.

Additional Resources

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

How to Remove a Legend in ggplot2
How to Remove Gridlines in ggplot2
How to Rotate Axis Labels in ggplot2

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories