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
r ggplot2 graphics


source to share


No one has answered this question yet

Check out similar questions:

50
How to fix aspect ratio in ggplot?
20
How to place mushrooms with annotation_custom () to precise areas of a plot parcel?
17
Save area with specified aspect ratio
4
ggplot2: set absolute distance to discrete point x
4
How can I manually put a fixed aspect ratio viewport in its parent so that space isn't wasted like ggplot?
3
Get aspect ratio for lengthy graphs
3
How do I store an image in R exactly as it is "stored"?
2
Aspect ratio of blocking on a part of the mesh
1
How to make segments that keep angles in different proportions in ggplot2?
0
Qmap aspect ratio



All Articles
Loading...
X
Show
Funny
Dev
Pics