Skip to content

Commit d61071d

Browse files
committed
Special grob for strips. Fixes #1286.
1 parent e6f57f5 commit d61071d

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ S3method(guide_merge,colorbar)
6969
S3method(guide_merge,legend)
7070
S3method(guide_train,colorbar)
7171
S3method(guide_train,legend)
72+
S3method(heightDetails,stripGrob)
7273
S3method(heightDetails,titleGrob)
7374
S3method(heightDetails,zeroGrob)
7475
S3method(interleave,default)
@@ -103,6 +104,7 @@ S3method(scale_type,logical)
103104
S3method(scale_type,numeric)
104105
S3method(str,uneval)
105106
S3method(summary,ggplot)
107+
S3method(widthDetails,stripGrob)
106108
S3method(widthDetails,titleGrob)
107109
S3method(widthDetails,zeroGrob)
108110
export("%+%")

R/facet-labels.r

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,16 @@ ggstrip <- function(text, horizontal = TRUE, theme) {
523523
text_theme <- if (horizontal) "strip.text.x" else "strip.text.y"
524524
if (is.list(text)) text <- text[[1]]
525525

526-
label <- element_render(theme, text_theme, text, expand_x = !horizontal,
527-
expand_y = horizontal)
526+
element <- calc_element(text_theme, theme)
527+
if (inherits(element, "element_blank"))
528+
return(zeroGrob())
529+
530+
gp <- gpar(fontsize = element$size, col = element$colour,
531+
fontfamily = element$family, fontface = element$face,
532+
lineheight = element$lineheight)
533+
534+
label <- stripGrob(text, element$hjust, element$vjust, element$angle,
535+
margin = element$margin, gp = gp, debug = element$debug)
528536

529537
ggname("strip", absoluteGrob(
530538
gList(
@@ -534,6 +542,7 @@ ggstrip <- function(text, horizontal = TRUE, theme) {
534542
width = grobWidth(label),
535543
height = grobHeight(label)
536544
))
545+
537546
}
538547

539548
# Helper to adjust angle of switched strips

R/margins.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,56 @@ widthDetails.titleGrob <- function(x) {
113113
heightDetails.titleGrob <- function(x) {
114114
sum(x$heights)
115115
}
116+
117+
# Works like titleGrob, but designed to place one label per viewport.
118+
# This means it doesn't have the lengths of labels available, so must use
119+
# alternative layout strategy
120+
stripGrob <- function(label, hjust, vjust, angle = 0, gp = gpar(),
121+
margin = NULL, debug = FALSE) {
122+
if (is.null(margin)) {
123+
margin <- margin()
124+
}
125+
126+
text_grob <- textGrob(label, rot = angle, gp = gp)
127+
128+
widths <- unit.c(margin[4], unit(1, "grobwidth", text_grob), margin[2])
129+
heights <- unit.c(margin[1], unit(1, "grobheight", text_grob), margin[3])
130+
131+
vp <- viewport(
132+
hjust, vjust, just = c(hjust, vjust),
133+
width = sum(widths),
134+
height = sum(heights),
135+
layout = grid.layout(3, 3, heights = heights, widths = widths),
136+
name = "top"
137+
)
138+
child_vp <- viewport(layout.pos.row = 2, layout.pos.col = 2)
139+
140+
if (debug) {
141+
children <- gList(
142+
rectGrob(gp = gpar(fill = "cornsilk", col = NA)),
143+
pointsGrob(unit(hjust, "npc"), unit(vjust, "npc"), pch = 20,
144+
gp = gpar(col = "gold")),
145+
text_grob
146+
)
147+
} else {
148+
children <- gList(text_grob)
149+
}
150+
151+
gTree(
152+
children = children,
153+
vp = vpTree(vp, vpList(child_vp)),
154+
widths = widths,
155+
heights = heights,
156+
cl = "stripGrob"
157+
)
158+
}
159+
160+
#' @export
161+
widthDetails.stripGrob <- function(x) {
162+
sum(x$widths)
163+
}
164+
165+
#' @export
166+
heightDetails.stripGrob <- function(x) {
167+
sum(x$heights)
168+
}

0 commit comments

Comments
 (0)