r-lib / gtable

The layout packages that powers ggplot2
https://gtable.r-lib.org
Other
87 stars 18 forks source link

Change default clip settings in gtable_col() and gtable_row()? #56

Open clauswilke opened 7 years ago

clauswilke commented 7 years ago

Both gtable_col() and gtable_row() have no clip parameter and set clip = "off" in the layout they generate. This clip setting switches off any clip settings that might have been chosen higher up:

# a circle grob
circ <- grid::circleGrob(gp = gpar(fill = "blue"))

# a function that scales a grob and clips it
scale_and_clip <- function(grob, size = .8, scale = 1.2) {
  vp_outer <- viewport(x = .5, y = .5,
                       width = size, height = size,
                       just = c("center", "center"),
                       clip = "on")

  vp_inner <- viewport(width = scale, height = scale,
                       just = c("center", "center"))

  inner_grob <- grobTree(grob, vp = vp_inner, name = "inner")
  outer_grob <- grobTree(inner_grob, vp = vp_outer, name = "outer")

  grid.newpage()
  grid.draw(outer_grob)
}

# this works correctly, top and bottom of the circle are cut off
circ <- grid::circleGrob(gp = gpar(fill = "blue"))
scale_and_clip(circ, .8, 1.2)

rplot1

# this doesn't work correctly, because gtable overrides the clip settings
u <- grid::unit(1, "null")
ct <- gtable::gtable_col(NULL, list(circ), u, u)
scale_and_clip(ct, .8, 1.2)

rplot2

# this fixes it
ct$layout$clip <- "inherit"
scale_and_clip(ct, .8, 1.2)
# output like the first image

# this breaks it again, because grid cannot nest clipping areas :-(
ct$layout$clip <- "on"
scale_and_clip(ct, .8, 1.2)
# output like the second image

I think both functions should have a clip parameter, and I also think the default should be "inherit" so that it is possible to clip gtables. I'm happy to generate a pull request if you agree. Not sure how this might interact with ggplot2, though. And it's frustrating that grid doesn't nest clipping areas, that really limits some of the more advanced applications of clipping.