Get the aspect ratio of the viewport in grid drawing mode

I need to design a new grid grid, which is an adjustment of the segments of the Grob, but allows the drawing to be completed at a given length (i.e. cut 1 cm of the segment from the end). Because the calculations are a function of both the segment angle and the aspect ratio of the viewport (in absolute dimensions). Calculations need to be pressed before drawing time using the drawDetails hook.

My question is how to get the absolute aspect ratio of the viewport in which the coffin is being drawn. The context I'm using this in is ggplot2, and what I get if I ask for current.viewport().width

or current.viewport().width

is 1npc (thus not in absolute dimensions).

Sample code

segmentsGrob2 <- function(x0 = unit(0, "npc"), y0 = unit(0, "npc"), x1 = unit(1, "npc"), y1 = unit(1, "npc"), startAdjust = unit(0, 'npc'), endAdjust = unit(0, 'npc'), default.units = "npc", arrow = NULL, name = NULL, gp = gpar(), vp = NULL) {
    if (!is.unit(x0)) 
        x0 <- unit(x0, default.units)
    if (!is.unit(x1)) 
        x1 <- unit(x1, default.units)
    if (!is.unit(y0)) 
        y0 <- unit(y0, default.units)
    if (!is.unit(y1)) 
        y1 <- unit(y1, default.units)
    grid.draw(grob(x0 = x0, y0 = y0, x1 = x1, y1 = y1, startAdjust=startAdjust, endAdjust=endAdjust, arrow = arrow, name=name, gp=gp, vp=vp, cl="segments2"))
}
drawDetails.segments2 <- function(x, ...) {
    asp <- getVpAspect() ### <-THIS IS WHAT I NEED

    # Do some modifications to x relative to asp

    grid:::drawDetails.segments(x, ...)
}

      

+3


source to share





All Articles