0

I have two tileplots:

library(ggplot2)

l1 <- 50
ncols <- 3
m1 <- matrix(rexp(l1*3, rate = .1), ncol = 1) %>% 
  as.data.frame() %>% 
  cbind(Comparison = rep(c("T1 vs C", "T2 vs C", "T3 vs C"), each = l1)) %>%
  cbind(ID = rep(stringi::stri_rand_strings(l1, 5), ncols))

p1 <- ggplot(m1, aes(x = Comparison, y = ID)) +
  geom_tile(aes(fill = V1), colour = "lightgray")

p1a <- egg::set_panel_size(p1, height=unit(nrow(m1)/2.5/3, "cm"),
                           width=unit(4, "cm"))

l2 <- 15
ncols <- 3
m2 <- matrix(rexp(l2*ncols, rate = .1), ncol = 1) %>% 
  as.data.frame() %>% 
  cbind(Comparison = rep(c("T1 vs C", "T2 vs C", "T3 vs C"), each = l2)) %>%
  cbind(ID = rep(stringi::stri_rand_strings(l2, 5), ncols))


p2 <- ggplot(m2, aes(x = Comparison, y = ID)) +
  geom_tile(aes(fill = V1), colour = "lightgray") 
p2a <- egg::set_panel_size(p2, height=unit(nrow(m2)/2.5/3, "cm"),
                           width=unit(4, "cm"))

I arrange them in 1 row. I tried two functions both with similar output (i.e. the position is different for the second but for both the edges of the larger plot is cut off).

p <- cowplot::plot_grid(p1a, p2a, nrow=1 )
p <- cowplot::plot_grid(p1a, arrangeGrob(nullGrob(), p2a, ncol = 1), nrow=1 )

Output is similar to this:

The problem is NOT when I save the image! The above is a saved image where I deliberately set the dimensions too small to show the problem. The problem arises in the output of this script which is a word document where the image is printed in, i.e:

print(p)

I tried to resolve by:

  1. find plot dimensions settings in plot_grid, but I didn't find.
  2. add a nullGrob() to the top of the whole plot (above both subplots) --> expactation: both subplots shift down. Didn't work.
  3. set the size of the nullGrob above p2a --> expectation: to make p2a the same height as p1a so plot_grid can gage better the full plot size. Didn't work
  4. set the plot.margin in the subplots, e.g.: p1 + theme(plot.margin = unit(c(5,5,5,5), "cm") This resulted in some shifting of the subplots but not in the right direction.

Thanks for help!

zephryl
  • 14,633
  • 3
  • 11
  • 30
schd
  • 3
  • 1
  • I also tried to resolve this using the two answers from here, but it didn't work. https://stackoverflow.com/questions/50201928/avoid-ggplot2-to-partially-cut-axis-text – schd Feb 13 '23 at 15:15

0 Answers0