How to remove tick labels in a ggplot without affecting the plot size?

Is there a way to save or display related ggplots with axes for each plot all the same size, but with some plots displaying the axis tick labels and other plots blanking the axis tick labels? The code below shows two ways I've removed the tick labels, but both resize the plots:

library(tidyverse)
library(cowplot)

tb <- tibble(a = 1:5, b = 1:5)

without_x_title <- ggplot(tb, aes(a,b)) +
  geom_point() +
  labs(x = "")

without_x_title_labels_1 <- ggplot(tb, aes(a,b)) +
  geom_point() +
  labs(x = "") +
  scale_x_discrete(labels = "")

without_x_title_labels_2 <- ggplot(tb, aes(a,b)) +
  geom_point() +
  labs(x = "") +
  theme(axis.text.x = element_blank())

ggdraw() +
  draw_plot(without_x_title, x = 0, y = 0, width = 0.3, height = 1) +
  draw_plot(without_x_title_labels_1, x = 0.3, y = 0, width = 0.3, height = 1) +
  draw_plot(without_x_title_labels_2, x = 0.6, y = 0, width = 0.3, height = 1)

I can fix this using cowplot::plot_grid or the patchwork package (see here), but I would like a fix that works at the level of the individual ggplots, for example so the axes appear the same size in the RStudio viewer or when I export/save them.

Any help would be greatly appreciated.

If you replace the labels with "" instead of removing them, the size should not change are there will be labels but nothing printed.

axis.text.x = element_blank() will remove the axis text element, so there is a resize.

scale_x_discrete(labels = "") is not correct here because you scale is continuous. It feels like a bug in ggplot that it deletes everything in x axis. :thinking:

Here how I would do it.

library(tidyverse)
#> Warning: le package 'tibble' a été compilé avec la version R 3.5.2
#> Warning: le package 'purrr' a été compilé avec la version R 3.5.2
#> Warning: le package 'stringr' a été compilé avec la version R 3.5.2
library(cowplot)
#> Warning: le package 'cowplot' a été compilé avec la version R 3.5.2
#> 
#> Attachement du package : 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

tb <- tibble(a = 1:5, b = 1:5)

without_x_title <- ggplot(tb, aes(a,b)) +
  geom_point() +
  labs(x = "")

# knowing how many break there is
without_x_title_labels_1 <- without_x_title +
  scale_x_continuous(labels = rep("", 5), breaks = 1:5)

# using a function of breaks
without_x_title_labels_2 <- without_x_title +
  scale_x_continuous(labels = function(breaks) {rep_along(breaks, "")})

ggdraw() +
  draw_plot(without_x_title, x = 0, y = 0, width = 0.3, height = 1) +
  draw_plot(without_x_title_labels_1, x = 0.3, y = 0, width = 0.3, height = 1) +
  draw_plot(without_x_title_labels_2, x = 0.6, y = 0, width = 0.3, height = 1)

Created on 2019-02-17 by the reprex package (v0.2.1)

2 Likes

Thanks @cderv. This is great, it's exactly what I was trying to do.

1 Like

If your question's been answered , would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Done. Would have done this before but I'm new to the site and hadn't noticed the check box. Thanks again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.