patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Gap between axis label and text if adjacent plot has vertical axis text

Open kotliary opened this issue 3 years ago • 6 comments

Please check the following code. Is there a way to remove this gap ignoring the other plots?

library(tidyverse)

df1 = data.frame(Group = factor(1:10), Value = runif(10))
df2 = data.frame(Group = factor(1:10), Long_label_A = runif(10), Long_label_B = runif(10), Long_label_C = runif(10)) %>% 
  gather(Label, X, -Group)

p1 = ggplot(df1, aes(Value, Group)) + 
  geom_col(orientation = "y", position = "identity")

p2 = ggplot(df2, aes(Label, Group, fill = X)) + 
  geom_tile() +
  ylab("") +
  theme_void() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

library(patchwork)

p1 + p2 + plot_layout(nrow = 1, widths = c(0.9, 0.1))

Created on 2021-12-01 by the reprex package (v2.0.0)

kotliary avatar Dec 01 '21 22:12 kotliary

It's the same issue as reported in #272 and #276.

kotliary avatar Feb 22 '22 17:02 kotliary

I have a similar issue, only horizontally. I found a quick and dirty workaround, maybe it will help someone, as the original positioning of the axis title looks ugly.

Original plot: obraz

and after the workaround:

final_plot[[1]] <- final_plot[[1]] + theme(axis.title.y = element_text(vjust = -50)) final_plot

obraz

Generalized avatar May 09 '22 15:05 Generalized

Good workaround. Thanks!

kotliary avatar May 09 '22 16:05 kotliary

@kotliary did the above solve your question or did you mean to expand the plot to fill out the white space?

thomasp85 avatar Aug 08 '23 07:08 thomasp85

@thomasp85 sorry for missing your question. No, I don't want to expand the plot to fill the white space. I want to move the axis title closer to the axis. The workaround from @Generalized solved my issue, but the vjust value need to be adjusted manually. It would be nice if the position of axis title can be automatically adjusted by the patchwork.

kotliary avatar Nov 07 '23 15:11 kotliary