Home > Blockchain >  Center-align shared y-axis text between plots
Center-align shared y-axis text between plots

Time:01-31

I have the following dummy data and plots:

library(ggplot2)
library(patchwork)

test_data <- data.frame(sample = c("sample1_long_label","sample2","sam3","sample4_long_labels_are_annoying"),
                        var1 = c(20,24,19,21),
                        var2 = c(4000, 3890, 4020, 3760))

p1 <- ggplot(test_data, aes(var1, sample))  
  geom_col()  
  scale_x_reverse()  
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank())

p2 <- ggplot(test_data, aes(var2, sample))  
  geom_col()  
  theme(axis.title.y = element_blank(),
        axis.ticks.y = element_blank())

p1   p2

This creates a plot that looks like this: enter image description here

Now, the y-axis labels are right-aligned to the right plot (they have been removed in the left plot). I would like the y-axis labels to be center-aligned between the two plots. Is this possible? If not, any other method that will create a similar plot is welcome.

Edit: I forgot to mention that the labels may be of different lengths, and have adjusted the test data. Is it possible to make the whole label visible in the center while center-adjusting the text?

CodePudding user response:

I think it might be easiest to control the spacing through the y-axis text margin, and discard any other margins or spacing. To do this:

  • Set the right margin of the left plot to 0
  • Set the left margin of the right plot to 0
  • Set the tick length of the right plot to 0. Even though these are blank, still space is reserved for them.
  • Set the right and left margins of the axis text of the right plot.

In the code below, 5.5 points is the default margin space, but feel free to adjust that to personal taste.

library(ggplot2)
library(patchwork)

test_data <- data.frame(sample = c("sample1","sample2","sample3","sample4"),
                        var1 = c(20,24,19,21),
                        var2 = c(4000, 3890, 4020, 3760))

p1 <- ggplot(test_data, aes(var1, sample))  
  geom_col()  
  scale_x_reverse()  
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        plot.margin = margin(5.5, 0, 5.5, 5.5))

p2 <- ggplot(test_data, aes(var2, sample))  
  geom_col()  
  theme(axis.title.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.length = unit(0, "pt"),
        plot.margin = margin(5.5, 5.5, 5.5, 0),
        axis.text.y.left = element_text(margin = margin(0, 5.5, 0, 5.5)))

p1   p2

Created on 2022-01-31 by the reprex package (v2.0.1)

EDIT: To center align labels with various lengths, you can use hjust as per usual: axis.text.y.left = element_text(margin = margin(0, 5.5, 0, 5.5), hjust = 0.5).

CodePudding user response:

A second option would be to add the axis labels via a third plot:

library(ggplot2)
library(patchwork)

test_data <- data.frame(sample = c("sample1","sample2","sample3","sample4"),
                        var1 = c(20,24,19,21),
                        var2 = c(4000, 3890, 4020, 3760))

p1 <- ggplot(test_data, aes(var1, sample))  
  geom_col()  
  scale_x_reverse()  
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank())

p2 <- ggplot(test_data, aes(var2, sample))  
  geom_col()  
  theme(axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank())

p3 <- ggplot(test_data, aes(1, sample))  
  geom_text(aes(label = sample))  
  scale_x_reverse()  
  theme_void()

p1   p3   p2  
  plot_layout(widths = c(4, 1, 4))

  •  Tags:  
  • Related