forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmargins.R
113 lines (94 loc) · 2.86 KB
/
margins.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#' Define margins.
#'
#' This is a convenience function that creates a grid unit object of the
#' correct length to use for setting margins.
#'
#' @export
#' @param t,b,r,l Dimensions of each margin. (To remember order, think trouble).
#' @param unit Default units of dimensions. Defaults to "pt" so it
#' can be most easily scaled with the text.
#' @export
#' @examples
#' margin(4)
#' margin(4, 2)
#' margin(4, 3, 2, 1)
margin <- function(t = 0, r = 0, b = 0, l = 0, unit = "pt") {
structure(unit(c(t, r, b, l), unit), class = c("margin", "unit"))
}
margin_height <- function(grob, margins) {
if (is.zero(grob)) return(unit(0, "cm"))
grobHeight(grob) + margins[1] + margins[3]
}
margin_width <- function(grob, margins) {
if (is.zero(grob)) return(unit(0, "cm"))
grobWidth(grob) + margins[2] + margins[4]
}
titleGrob <- function(label, x, y, hjust, vjust, angle = 0, gp = gpar(),
margin = NULL, expand_x = FALSE, expand_y = FALSE,
debug = FALSE) {
if (is.null(label))
return(zeroGrob())
if (is.null(margin)) {
margin <- margin(0, 0, 0, 0)
}
angle <- angle %% 360
if (angle == 90) {
xp <- 1 - vjust
yp <- hjust
} else if (angle == 180) {
xp <- 1 - hjust
yp <- 1 - vjust
} else if (angle == 270) {
xp <- vjust
yp <- 1 - hjust
} else {
xp <- hjust
yp <- vjust
}
x <- x %||% unit(xp, "npc")
y <- y %||% unit(yp, "npc")
text_grob <- textGrob(label, x, y, hjust = hjust, vjust = vjust,
rot = angle, gp = gp)
if (expand_x && expand_y) {
widths <- unit.c(margin[4], unit(1, "grobwidth", text_grob), margin[2])
heights <- unit.c(margin[1], unit(1, "grobheight", text_grob), margin[3])
vp <- viewport(layout = grid.layout(3, 3, heights = heights, widths = widths), gp = gp)
child_vp <- viewport(layout.pos.row = 2, layout.pos.col = 2)
} else if (expand_x) {
widths <- unit.c(margin[4], unit(1, "grobwidth", text_grob), margin[2])
vp <- viewport(layout = grid.layout(1, 3, widths = widths), gp = gp)
child_vp <- viewport(layout.pos.col = 2)
heights <- unit(1, "null")
} else if (expand_y) {
heights <- unit.c(margin[1], unit(1, "grobheight", text_grob), margin[3])
vp <- viewport(layout = grid.layout(3, 1, heights = heights), gp = gp)
child_vp <- viewport(layout.pos.row = 2)
widths <- unit(1, "null")
} else {
return(text_grob)
}
if (debug) {
children <- gList(
rectGrob(gp = gpar(fill = "grey90")),
pointsGrob(x[1], y[1], pch = 20, gp = gpar(col = "red")),
text_grob
)
} else {
children <- gList(text_grob)
}
gTree(
children = children,
vp = vpTree(vp, vpList(child_vp)),
widths = widths,
heights = heights,
cl = "titleGrob"
)
}
#' @export
widthDetails.titleGrob <- function(x) {
sum(x$widths)
}
#' @export
heightDetails.titleGrob <- function(x) {
sum(x$heights)
}