Skip to content

Commit 49d438c

Browse files
authored
fix title_spec() for vectorized input (#3493)
* make font_descent() not vectorized. closes #3492. * work around vectorized angles * Add warning when element_text() is used with vectorized input. * add news item
1 parent b68912d commit 49d438c

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ggplot2 (development version)
22

3+
* `element_text()` now issues a warning when vectorized arguments are provided, as in
4+
`colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported
5+
(@clauswilke, #3492).
6+
37
* stacking text when calculating the labels and the y axis with
48
`stat_summary()` now works (@ikosmidis, #2709)
59

R/margins.R

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(),
7171
# Use trigonometry to calculate grobheight and width for rotated grobs. This is only
7272
# exactly correct when vjust = 1. We need to take the absolute value so we don't make
7373
# the grob smaller when it's flipped over.
74-
text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle / 180 * pi)) * descent
75-
text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle / 180 * pi)) * descent
74+
text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle[1] / 180 * pi)) * descent
75+
text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle[1] / 180 * pi)) * descent
7676

7777
if (isTRUE(debug)) {
7878
children <- gList(
@@ -333,29 +333,36 @@ rotate_just <- function(angle, hjust, vjust) {
333333
list(hjust = hnew, vjust = vnew)
334334
}
335335
descent_cache <- new.env(parent = emptyenv())
336+
# Important: This function is not vectorized. Do not use to look up multiple
337+
# font descents at once.
336338
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
337339
cur_dev <- names(grDevices::dev.cur())
340+
if (cur_dev == "null device") {
341+
cache <- FALSE # don't cache if no device open
342+
} else {
343+
cache <- TRUE
344+
}
338345
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)
339-
descents <- lapply(key, function(k) {
340-
descent <- descent_cache[[k]]
341-
342-
if (is.null(descent)) {
343-
descent <- convertHeight(grobDescent(textGrob(
344-
label = "gjpqyQ",
345-
gp = gpar(
346-
fontsize = size,
347-
cex = cex,
348-
fontfamily = family,
349-
fontface = face
350-
)
351-
)), 'inches')
352-
descent_cache[[k]] <- descent
346+
# we only look up the first result; this function is not vectorized
347+
key <- key[1]
348+
349+
descent <- descent_cache[[key]]
350+
351+
if (is.null(descent)) {
352+
descent <- convertHeight(grobDescent(textGrob(
353+
label = "gjpqyQ",
354+
gp = gpar(
355+
fontsize = size,
356+
cex = cex,
357+
fontfamily = family,
358+
fontface = face
359+
)
360+
)), 'inches')
361+
362+
if (cache) {
363+
descent_cache[[key]] <- descent
353364
}
354-
descent
355-
})
356-
if (length(descents) == 1) {
357-
descents[[1]]
358-
} else {
359-
do.call(unit.c, descents)
360365
}
366+
367+
descent
361368
}

R/theme-elements.r

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ element_text <- function(family = NULL, face = NULL, colour = NULL,
112112
color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE) {
113113

114114
if (!is.null(color)) colour <- color
115+
116+
n <- max(
117+
length(family), length(face), length(colour), length(size),
118+
length(hjust), length(vjust), length(angle), length(lineheight)
119+
)
120+
if (n > 1) {
121+
warning(
122+
"Vectorized input to `element_text()` is not officially supported.\n",
123+
"Results may be unexpected or may change in future versions of ggplot2.",
124+
call. = FALSE
125+
)
126+
}
127+
128+
115129
structure(
116130
list(family = family, face = face, colour = colour, size = size,
117131
hjust = hjust, vjust = vjust, angle = angle, lineheight = lineheight,

0 commit comments

Comments
 (0)