Search code examples
rgridgrobgmisc

horizontal connectGrobs don't always connect to the edge of the boxGrob


I was running the example here, and noticed that the horizontal arrow connecting the Total to the Ineligible boxGrobs doesn't always touch the left-edge of the the Ineligible boxGrob.

It seems to depend on the width of the viewing window in RStudio. This does not seem to be the case for the vertical arrow, which always seem to perfectly connect to the top of the correct boxGrob.

Is there a way to force the arrow to touch the side of the box and not go any further? I am trying to save the output to a pdf, and by default it seems to use a wider plotting window so all of my horizontal arrows don't align with the correct boxes.

Narrow plotting window:

image

Wide plotting window:

image

I have tried manually creating a viewport with a wider area, but that didn't change anything in the pdf:

Code:

library(grid)
library(Gmisc)

vp <- grid::viewport(x = 10, y = 10, clip = 'on', xscale = c(0, 10), 
                     yscale = c(0, 10), default.units = 'inch') 
grid::pushViewport(vp)

leftx  <- .25
midx   <- .5
rightx <- .75
width  <- .4
gp <- gpar(fill = "lightgrey")

# add box/connectors to the plot
(total <- boxGrob("Total\n N = NNN", 
 x=midx, y=.9, box_gp = gp, width = width))
(rando <- boxGrob("Randomized\n N = NNN", 
 x=midx, y=.75, box_gp = gp, width = width))

connectGrob(total, rando, "v")

(inel <- boxGrob("Ineligible\n N = NNN", 
 x=rightx, y=.825, box_gp = gp, width = .25, height = .05))

connectGrob(total, inel, "-")

Solution

  • For the time being, this problem can be solved using absolute unit.

    Example code:

    (inel <- boxGrob("Ineligible\n N = NNN", 
                     x=rightx, y=.825, box_gp = gp, width = unit(2, "inch"), height = .05))