I create plots for automated reports with
Some plots have a
In order to add my plot to my docx report without unwanted margins due to a bad choice of height and width, I would like to evaluate first the ratio between the height and the width (of the entire plot including legend and title).
library(ggplot2) g <- ggplot(mtcars, aes(mpg, qsec, colour = disp, shape = factor(carb))) + geom_point() + coord_equal() + theme(legend.position = "top", plot.margin = margin(1, 1, 1, 1, unit = "lines")) + ggtitle("Mtcars")
The ratio of this example height / width is visually equal to 0.55.
It's more evident with gtable that show the entire plot layout in blue:
g_g <- ggplotGrob(g) gtable::gtable_show_layout(g_g)
I have not found in the list of
ggplotGrob(g) nor in
gtable_show_layout(g_g) any way to find the ratio between height and width.
gtable_show_layout know however to print it correctly !
When I try to sum the height of rows and width of cols and convert it, the results are quite confusing.
h <- grid::convertUnit(gtable::gtable_height(g_g), 'inches', valueOnly = TRUE) w <- grid::convertUnit(gtable::gtable_width(g_g), 'inches', valueOnly = TRUE) h/w # = 2.27 # with absolute.size h <- grid::convertUnit(grid::absolute.size(gtable::gtable_height(g_g)), 'inches', valueOnly = TRUE) w <- grid::convertUnit(grid::absolute.size(gtable::gtable_width(g_g)), 'inches', valueOnly = TRUE) h/w # = 2.79
Anybody can tell me where I'm wrong ?