patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

inset_element adding margins?

Open r2evans opened this issue 3 years ago • 5 comments

Reprex:

library(ggplot2)   # 3.3.3
library(patchwork) # 1.1.1
gg1 <- ggplot(expand.grid(x=0:1, y=0:1), aes(x, y)) +
  geom_path(size = 2) +
  coord_cartesian(expand = 0) +
  theme(
   ,axis.title = element_blank()
   ,axis.text = element_blank()
   ,axis.ticks = element_blank()
  )    
gg2 <- gg1 +
  theme(
   ,panel.background = element_rect(fill = "transparent") # bg of the panel
   ,plot.background = element_rect(fill = "transparent", color = NA) # bg of the plot
   ,panel.grid.major = element_blank() # get rid of major grid
   ,panel.grid.minor = element_blank() # get rid of minor grid
   ,legend.background = element_rect(fill = "transparent") # get rid of legend bg
   ,legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
  )

gg1 is straight-forward:

image

When we inset_element(gg2,...) in the upper-right corner, though,

gg1 +
  inset_element(gg2, left = 0.8, right = 1, bottom = 0.8, top = 1)

image

Two problems with this:

  • the full-plot margins have shifted inwards, and
  • the inset plot does not appear to extend from =0.8 to =1 in either axis; I would expect a bit of an up/right shift as in

image

(plus not moving the outer margins of the full-plot).

Slightly related, if I remove the panel.background and plot.background theme elements, we'll see a bit more:

gg3 <- gg1 +
  theme(
   #  panel.background = element_rect(fill = "transparent") # bg of the panel
   # ,plot.background = element_rect(fill = "transparent", color = NA) # bg of the plot
   ,panel.grid.major = element_blank() # get rid of major grid
   ,panel.grid.minor = element_blank() # get rid of minor grid
   ,legend.background = element_rect(fill = "transparent") # get rid of legend bg
   ,legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
  )
gg1 +
  inset_element(gg3, left = 0.8, right = 1, bottom = 0.8, top = 1)

image

showing an added white boundary. In addition to the expected up/right shift from above, I did not expect the white boundary.

Is there a way to maintain the full-plot margins, and to make sure that top=1 means the top border of the plot region, not just near it?

r2evans avatar Jan 28 '21 18:01 r2evans

Not sure I understand - what you are seeing is the plot margin, which is controllable through theming

e.g. gg2 + theme(plot.margin = margin(0, 0, 0, 0)) should give you what you want, right?

thomasp85 avatar Aug 02 '21 09:08 thomasp85

The issue is that the margins are different based on the presence of an inset. If we look at just one corner (red border added externally for clarity), we see

image

where the top image is no-inset and the bottom has had an inset applied (but not visible in this cropping). They should line up.

Your theme(.) correction adjusts the inset image but does not fix the main image's margins being changed.

Bottom line, my expectation is that adding an inset should not have side-effect (other than masking) on the main image.

r2evans avatar Aug 02 '21 12:08 r2evans

Oh - I misunderstood the issue...

What you are seeing is that patchwork's get their own margin in order to account for top level titles etc. For single plot patchwork's this looks like a doubling of the margin but is in reality two separate margins at play, one for the plot and one for the patchwork.

thomasp85 avatar Aug 02 '21 13:08 thomasp85

That makes sense, thanks. Since no overarching title was added, is there a way to prevent or control this double-margin?

r2evans avatar Aug 02 '21 16:08 r2evans

Yes, through the theme argument in plot_annotation()

thomasp85 avatar Aug 02 '21 19:08 thomasp85