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.
cderv
February 17, 2019, 9:29am
2
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.
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
cderv
February 18, 2019, 7:29am
4
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:
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Done. Would have done this before but I'm new to the site and hadn't noticed the check box. Thanks again.
1 Like
system
Closed
February 25, 2019, 9:08am
6
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.