6

I am trying to arrange a png image and a ggplot object side by side using plot_grid from cowplot package:

library(cowplot)
library(tableHTML)

sg <- data.frame(start = c(0,5,10), 
                 end = c(10,20,30),
                 duration = c(10,15,20), 
                 row.names = c("A","B","C"))

p1 <- ggplot(sg)+
  geom_segment(aes(x=start, y=row_number(start), xend=end, yend=row_number(start)), 
               size = 3) +
  scale_y_reverse(labels = NULL) +
  labs(y = "", x = "") +
  theme(aspect.ratio = 1/5)

sg %>%
  tableHTML() %>%
  add_theme("scientific") %>%
  tableHTML_to_image(file = "image2.png", zoom = 2)

p2 <- ggdraw() + draw_image("image2.png", scale = 0.7)

plot_grid(p2, p1, ncol = 2, scale = c(0.9,1), rel_widths = c(1, 2))

However, in the resulting plot I find it difficult to arrange the two plots as required.

see here

Ideally, I would like the lower border of the table image to be in line with the x-axis of the graph. I would also like the two items to be closer to each other.

I have tried modifying the scale of plot p2, the zoom argument in the tableHTML_to_image command, or the scale, rel_widths, align and axis arguments within the plot_grid command, but none of it really gets me closer to the desired output.

I also read here: Insert Image/PNG ggplot2 - Cowplot but that's only for the adding an image inside an existing plot, not beside it.

While plot_grid works fine when arranging two ggplots, I can't get it right with an image and a ggplot.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Ant
  • 113
  • 10

0 Answers0