Skip to content

Commit 4e7c54a

Browse files
Merge pull request #13 from teunbrand/ggplot2_3.5.0
inline `add_margins()`
2 parents b810ebc + 05c8625 commit 4e7c54a

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

R/element_shadowtext.R

+48-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,53 @@ shadow.title_spec <- function (label, x, y, hjust, vjust, angle, gp = gpar(), de
106106
"%||%" <- getFromNamespace("%||%", "ggplot2")
107107
zeroGrob <- getFromNamespace("zeroGrob", "ggplot2")
108108
modify_list <- getFromNamespace("modify_list", "ggplot2")
109-
add_margins <- getFromNamespace("add_margins", "ggplot2")
110109
rotate_just <- getFromNamespace("rotate_just", "ggplot2")
111110
font_descent <- getFromNamespace("font_descent", "ggplot2")
111+
112+
add_margins <- function(grob, height, width, margin = NULL,
113+
gp = gpar(), margin_x = FALSE, margin_y = FALSE) {
114+
115+
if (is.null(margin)) {
116+
margin <- margin(0, 0, 0, 0)
117+
}
118+
119+
if (margin_x & margin_y) {
120+
widths <- grid::unit.c(margin[4], width, margin[2])
121+
heights <- grid::unit.c(margin[1], height, margin[3])
122+
123+
vp <- grid::viewport(
124+
layout = grid::grid.layout(3, 3, heights = heights, widths = widths),
125+
gp = gp
126+
)
127+
child_vp <- grid::viewport(layout.pos.row = 2, layout.pos.col = 2)
128+
} else if (margin_x) {
129+
widths <- grid::unit.c(margin[4], width, margin[2])
130+
vp <- grid::viewport(layout = grid::grid.layout(1, 3, widths = widths), gp = gp)
131+
child_vp <- grid::viewport(layout.pos.col = 2)
132+
heights <- unit(1, "null")
133+
} else if (margin_y) {
134+
heights <- grid::unit.c(margin[1], height, margin[3])
135+
vp <- grid::viewport(layout = grid::grid.layout(3, 1, heights = heights), gp = gp)
136+
child_vp <- grid::viewport(layout.pos.row = 2)
137+
widths <- unit(1, "null")
138+
} else {
139+
widths <- width
140+
heights <- height
141+
return(
142+
grid::gTree(
143+
children = grob,
144+
widths = widths,
145+
heights = heights,
146+
cl = "titleGrob"
147+
)
148+
)
149+
}
150+
151+
grid::gTree(
152+
children = grob,
153+
vp = grid::vpTree(vp, grid::vpList(child_vp)),
154+
widths = widths,
155+
heights = heights,
156+
cl = "titleGrob"
157+
)
158+
}

0 commit comments

Comments
 (0)