Skip to content

fix title_spec() for vectorized input #3493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* `element_text()` now issues a warning when vectorized arguments are provided, as in
`colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported
(@clauswilke, #3492).

* stacking text when calculating the labels and the y axis with
`stat_summary()` now works (@ikosmidis, #2709)

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

if (isTRUE(debug)) {
children <- gList(
Expand Down Expand Up @@ -333,29 +333,36 @@ rotate_just <- function(angle, hjust, vjust) {
list(hjust = hnew, vjust = vnew)
}
descent_cache <- new.env(parent = emptyenv())
# Important: This function is not vectorized. Do not use to look up multiple
# font descents at once.
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
cur_dev <- names(grDevices::dev.cur())
if (cur_dev == "null device") {
cache <- FALSE # don't cache if no device open
} else {
cache <- TRUE
}
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)
descents <- lapply(key, function(k) {
descent <- descent_cache[[k]]

if (is.null(descent)) {
descent <- convertHeight(grobDescent(textGrob(
label = "gjpqyQ",
gp = gpar(
fontsize = size,
cex = cex,
fontfamily = family,
fontface = face
)
)), 'inches')
descent_cache[[k]] <- descent
# we only look up the first result; this function is not vectorized
key <- key[1]

descent <- descent_cache[[key]]

if (is.null(descent)) {
descent <- convertHeight(grobDescent(textGrob(
label = "gjpqyQ",
gp = gpar(
fontsize = size,
cex = cex,
fontfamily = family,
fontface = face
)
)), 'inches')

if (cache) {
descent_cache[[key]] <- descent
}
descent
})
if (length(descents) == 1) {
descents[[1]]
} else {
do.call(unit.c, descents)
}

descent
}
14 changes: 14 additions & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ element_text <- function(family = NULL, face = NULL, colour = NULL,
color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE) {

if (!is.null(color)) colour <- color

n <- max(
length(family), length(face), length(colour), length(size),
length(hjust), length(vjust), length(angle), length(lineheight)
)
if (n > 1) {
warning(
"Vectorized input to `element_text()` is not officially supported.\n",
"Results may be unexpected or may change in future versions of ggplot2.",
call. = FALSE
)
}


structure(
list(family = family, face = face, colour = colour, size = size,
hjust = hjust, vjust = vjust, angle = angle, lineheight = lineheight,
Expand Down