• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Remove X Axis Tick and Axis Text with ggplot2 in R?

datavizpyr · January 31, 2020 ·

Sometimes when you make a plot in R you might want the plot without ticks and text on axis. In this tutorial, we will learn how to remove x-axis or y-axis ticks and the text in R with ggplot2.

Let us first load tidyverse, a suite of R packages from RStudio. Let us also load gapminder R package as we will be using gapminder data to make the plot with x-axis ticks and text.

library(tidyverse)
library(gapminder)
theme_set(theme_bw(base_size=16))

We will use a subset of gapminder data containing countries from Africa. Let us make the year variable as factor.

df1 <- gapminder %>% 
  filter(continent %in% c("Africa")) %>%
  mutate(year=factor(year))

Our example plot in this post is a heatmap of lifeExp over years for african countries. We can make heatmap from tidy data using ggplot2’s geom_tile() function. We have country on y-axis and year on x-axis and the values of heatmap are lifeExp.

df1 %>%
  ggplot(aes(y=country, x=year, fill=lifeExp)) + 
  geom_tile() +
  scale_fill_viridis_c()

Note that the simple heatmap we made has both x-axis and y-axis ticks and text. x and y-axis ticks are the tiny black lines. And the x-axis texts for its ticks is the year values on x-axis.

A plot with Axis Tick and Axis Text in ggplot2
A plot with Axis Tick and Axis Text in ggplot2

Remove Axes Text/Tick in ggplot2

We can remove axis ticks and texts using the theme function in ggplot2. The theme() function in ggplot2 is a powerful function that allows users to customize various aspects of ggplot2 theme including the axis ticks and texts.

To remove x-axis ticks we specify the argument axis.ticks.x = element_blank() inside the theme(). And similarly to remove x-axis text, we specify axis.text.x = element_blank().

df1 %>%
  ggplot(aes(y=country, x=year, fill=lifeExp)) + 
  geom_tile() +
  scale_fill_viridis_c() + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank())

Now we have the heatmap with no x-axis ticks and texts. We can also easily remove y-axis ticks and texts using axis.ticks.y and axis.text.y arguments.

A plot without x-axis tick and x-axis Text in ggplot2
Remove x-axis tick and x-axis Text in ggplot2

Related posts:

Dollar format ggplot2How to Add Dollar Symbol for Axis Labels with ggplot2? Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R Annotate Clusters with Ellipse with Labels ggforceHow To Annotate Clusters with Circle/Ellipse by a Variable in R

Filed Under: ggplot2, R, remove axis tick and text Tagged With: ggplot2, R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version