'Understanding how margins affect plot width in patchwork
I was wanting to understand how patchwork sizes the plots when they have different margins. (I am making plots for a web application and want to control the pixel widths of the plots precisely). The plot_layout function in patchwork has a widths argument, but this appears to control the panel widths, rather than the plot widths. I would like to set the widths so that plots which have different margins end up the same size. For a specific example, in the code below, how do I calculate the widths argument to plot_layout so that the boundary between the plots is in the center of the image:
library(ggplot2)
library(patchwork)
g <- ggplot(data.frame(x=c(1, 2, 3), y=c(1, 2, 3)), aes(x=x, y=y)) + geom_point()
# Plots with different margins
ggsave((g + theme(plot.background=element_rect(colour='red'),
plot.margin=margin(0, 1, 0, 0, "inches"))) +
(g + theme(plot.background=element_rect(colour='blue'))) +
plot_layout(nrow=1, widths=c(1, 1)),
width=6,
height=3,
file='margins.png',
dpi=100)
When the plots have different margins, then the panels are the same size, but the plots have different sizes:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

