Search code examples
rggplot2layoutpatchwork

patchwork do not reduce white space between two ggplot2


I have this plot created from df1 and df2

enter image description here

I tried many attempts to reduce the white space encircled by red. How can I achieve this? There is a similar thread on SO, which I followed, but that did not solve it:

First:

who1pred <- ggplot(filter(df1), 
                   aes(x = ki67, y = pred, color = time, fill = time)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = NA) +
  geom_line() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))  + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
    #plot.margin = unit(c(5.5, 5.5, 0, 5.5), "pt")
        )


who1dist <- ggplot(filter(df2), 
                   aes(x = Ki67)) +
  geom_histogram() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50)) #+
  #theme(plot.margin = unit(c(0, 5.5, 0, 5.5), "pt"))

The basic plot:

who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4))  

I tried the following (and some variations of it)

who1pred / plot_spacer() / who1dist +
  plot_layout(ncol = 1, heights = c(1, 0, .4),
              guides = "collect")

I tried adjusting theme(plot.margin = ..) in the ggplots.

I also tried the following with different values in plot.margin.

who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4) & theme(plot.margin = c(0, 0, 0, 0)))

Any solutions?

df1 <- structure(list(WHO = c("2", "2", "3", "1", "1", "2", "2", "3", 
                              "1", "3"), ki67 = c(94, 64, 51, 15, 18, 74, 93, 45, 65, 62), 
                      pred = c(0.464410116302291, 0.897401711241776, 0.700242375617693, 
                               0.358678057582871, 0.390573594975723, 0.829113422265248, 
                               0.48802465997491, 0.666022132399912, 0.540540217190029, 0.699322187068211
                      ), time = c("120", "120", "120", "120", "120", "120", "120", 
                                  "120", "120", "60"), lower = c(0.0137577850103707, 0.785671003373972, 
                                                                 0.540479287582779, 0.282505841174618, 0.282542223489564, 
                                                                 0.654223374533307, 0.0466809675600842, 0.490877830284619, 
                                                                 0, 0.550598433238934), upper = c(0.915062447594212, 1, 0.860005463652608, 
                                                                                                  0.434850273991124, 0.498604966461882, 1, 0.929368352389736, 
                                                                                                  0.841166434515205, 1, 0.848045940897488)), row.names = c(NA, 
                                                                                                                                                           -10L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- dput(sample_n(select(d_WHO, Ki67, WHO), 10))
structure(list(Ki67 = c(1, 2, 5, 11, 1, 1, 8, 1, 4, 2), WHO = c("1", 
                                                                "1", "2", "2", "1", "1", "2", "1", "1", "2")), row.names = c(NA, 
                                                                                                                             -10L), class = "data.frame")

Solution

  • You are almost there. At the moment you have set the x axis title to "". This requires as much height as any other axis title. You just need to set axis.title.x = element_blank() on the first plot to remove the axis title completely.