Skip to content

Calculate constant descender heights, regardless of label content. #2471

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 6 commits into from
May 1, 2018
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
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ up correct aspect ratio, and draws a graticule.

* Default themes use `rel()` to set line widths (@baptiste).

* Themes were tweaked for visual consistency and more graceful behavior when changing
the base font size. All absolute heights or widths were replaced with heights or
widths that are proportional to the base font size. One relative font size
was eliminated. (@clauswilke)

### Other

* `fortify()` gains a method for tbls (@karawoo, #2218)
Expand Down Expand Up @@ -324,6 +329,8 @@ up correct aspect ratio, and draws a graticule.
summarise the layout, coordinate systems, and layers, of a built ggplot object
(#2034, @wch). This provides a tested API that (e.g.) shiny can depend on.

* The height of descenders is now calculated solely on font metrics and doesn't change with the specific letters in the string. This fixes minor alignment issues with plot titles, subtitles, and legend titles. (#2288, @clauswilke)

* Update startup messages to reflect new resources. (#2410, @mine-cetinkaya-rundel)

# ggplot2 2.2.1
Expand Down
13 changes: 11 additions & 2 deletions R/guide-colorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,14 @@ guide_gengrob.colorbar <- function(guide, theme) {
if (!guide$draw.llim) tic_pos.c <- tic_pos.c[-length(tic_pos.c)]

# title

# obtain the theme for the legend title. We need this both for the title grob
# and to obtain the title fontsize.
title.theme <- guide$title.theme %||% calc_element("legend.title", theme)

grob.title <- ggname("guide.title",
element_grob(
guide$title.theme %||% calc_element("legend.title", theme),
title.theme,
label = guide$title,
hjust = guide$title.hjust %||% theme$legend.title.align %||% 0,
vjust = guide$title.vjust %||% 0.5
Expand All @@ -296,10 +301,14 @@ guide_gengrob.colorbar <- function(guide, theme) {
title_width.c <- c(title_width)
title_height <- convertHeight(grobHeight(grob.title), "mm")
title_height.c <- c(title_height)
title_fontsize <- title.theme$size
if (is.null(title_fontsize)) title_fontsize <- 0

# gap between keys etc
hgap <- width_cm(theme$legend.spacing.x %||% unit(0.3, "line"))
vgap <- height_cm(theme$legend.spacing.y %||% (0.5 * unit(title_height, "cm")))
# multiply by 5 instead of 0.5 due to unit error below. this needs to be fixed
# separately (pull request pending).
vgap <- height_cm(theme$legend.spacing.y %||% (5 * unit(title_fontsize, "pt")))

# label
label.theme <- guide$label.theme %||% calc_element("legend.text", theme)
Expand Down
10 changes: 8 additions & 2 deletions R/guide-legend.r
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,13 @@ guide_gengrob.legend <- function(guide, theme) {

nbreak <- nrow(guide$key)

# obtain the theme for the legend title. We need this both for the title grob
# and to obtain the title fontsize.
title.theme <- guide$title.theme %||% calc_element("legend.title", theme)

grob.title <- ggname("guide.title",
element_grob(
guide$title.theme %||% calc_element("legend.title", theme),
title.theme,
label = guide$title,
hjust = guide$title.hjust %||% theme$legend.title.align %||% 0,
vjust = guide$title.vjust %||% 0.5,
Expand All @@ -338,10 +342,12 @@ guide_gengrob.legend <- function(guide, theme) {

title_width <- width_cm(grob.title)
title_height <- height_cm(grob.title)
title_fontsize <- title.theme$size
if (is.null(title_fontsize)) title_fontsize <- 0

# gap between keys etc
hgap <- width_cm(theme$legend.spacing.x %||% unit(0.3, "line"))
vgap <- height_cm(theme$legend.spacing.y %||% (0.5 * unit(title_height, "cm")))
vgap <- height_cm(theme$legend.spacing.y %||% (0.5 * unit(title_fontsize, "pt")))

# Labels
if (!guide$label || is.null(guide$key$.label)) {
Expand Down
19 changes: 14 additions & 5 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(),
gp = gp
)

# The grob dimensions don't include the text descenders, so add on using
# a little trigonometry. This is only exactly correct when vjust = 1.
descent <- descentDetails(text_grob)
text_height <- unit(1, "grobheight", text_grob) + cos(angle / 180 * pi) * descent
text_width <- unit(1, "grobwidth", text_grob) + sin(angle / 180 * pi) * descent
# The grob dimensions don't include the text descenders, so these need to be added
# manually. Because descentDetails calculates the actual descenders of the specific
# text label, which depends on the label content, we replace the label with one that
# has the common letters with descenders. This guarantees that the grob always has
# the same height regardless of whether the text actually contains letters with
# descenders or not. The same happens automatically with ascenders already.
temp <- editGrob(text_grob, label = "gjpqyQ")
descent <- descentDetails(temp)

# 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

if (isTRUE(debug)) {
children <- gList(
Expand Down
92 changes: 50 additions & 42 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,19 @@ NULL
theme_grey <- function(base_size = 11, base_family = "",
base_line_size = base_size / 22,
base_rect_size = base_size / 22) {

# The half-line (base-fontsize / 2) sets up the basic vertical
# rhythm of the theme. Most margins will be set to this value.
# However, when we work with relative sizes, we may want to multiply
# `half_line` with the appropriate relative size. This applies in
# particular for axis tick sizes. And also, for axis ticks and
# axis titles, `half_size` is too large a distance, and we use `half_size/2`
# instead.
half_line <- base_size / 2

# Throughout the theme, we use three font sizes, `base_size` (`rel(1)`)
# for normal, `rel(0.8)` for small, and `rel(1.2)` for large.

theme(
# Elements in this first block aren't used directly, but are inherited
# by others
Expand Down Expand Up @@ -100,29 +111,29 @@ theme_grey <- function(base_size = 11, base_family = "",
axis.ticks = element_line(colour = "grey20"),
axis.ticks.length = unit(half_line / 2, "pt"),
axis.title.x = element_text(
margin = margin(t = half_line),
margin = margin(t = half_line / 2),
vjust = 1
),
axis.title.x.top = element_text(
margin = margin(b = half_line),
margin = margin(b = half_line / 2),
vjust = 0
),
axis.title.y = element_text(
angle = 90,
margin = margin(r = half_line),
margin = margin(r = half_line / 2),
vjust = 1
),
axis.title.y.right = element_text(
angle = -90,
margin = margin(l = half_line),
margin = margin(l = half_line / 2),
vjust = 0
),

legend.background = element_rect(colour = NA),
legend.spacing = unit(0.4, "cm"),
legend.spacing = unit(2 * half_line, "pt"),
legend.spacing.x = NULL,
legend.spacing.y = NULL,
legend.margin = margin(0.2, 0.2, 0.2, 0.2, "cm"),
legend.margin = margin(half_line, half_line, half_line, half_line),
legend.key = element_rect(fill = "grey95", colour = "white"),
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
Expand All @@ -137,7 +148,7 @@ theme_grey <- function(base_size = 11, base_family = "",
legend.box = NULL,
legend.box.margin = margin(0, 0, 0, 0, "cm"),
legend.box.background = element_blank(),
legend.box.spacing = unit(0.4, "cm"),
legend.box.spacing = unit(2 * half_line, "pt"),

panel.background = element_rect(fill = "grey92", colour = NA),
panel.border = element_blank(),
Expand All @@ -152,31 +163,30 @@ theme_grey <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = "grey10",
size = rel(0.8),
margin = margin(half_line, half_line, half_line, half_line)
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
),
strip.text.x = NULL,
strip.text.y = element_text(angle = -90),
strip.placement = "inside",
strip.placement.x = NULL,
strip.placement.y = NULL,
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
strip.switch.pad.grid = unit(half_line / 2, "pt"),
strip.switch.pad.wrap = unit(half_line / 2, "pt"),

plot.background = element_rect(colour = "white"),
plot.title = element_text(
plot.title = element_text( # font size "large"
size = rel(1.2),
hjust = 0, vjust = 1,
margin = margin(b = half_line * 1.2)
margin = margin(b = half_line)
),
plot.subtitle = element_text(
size = rel(0.9),
plot.subtitle = element_text( # font size "regular"
hjust = 0, vjust = 1,
margin = margin(b = half_line * 0.9)
margin = margin(b = half_line)
),
plot.caption = element_text(
size = rel(0.9),
plot.caption = element_text( # font size "small"
size = rel(0.8),
hjust = 1, vjust = 1,
margin = margin(t = half_line * 0.9)
margin = margin(t = half_line)
),
plot.margin = margin(half_line, half_line, half_line, half_line),

Expand Down Expand Up @@ -248,7 +258,7 @@ theme_linedraw <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = "white",
size = rel(0.8),
margin = margin(half_line, half_line, half_line, half_line)
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -290,7 +300,7 @@ theme_light <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = "white",
size = rel(0.8),
margin = margin(half_line, half_line, half_line, half_line)
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -332,7 +342,7 @@ theme_dark <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = "grey90",
size = rel(0.8),
margin = margin(half_line, half_line, half_line, half_line)
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -420,25 +430,24 @@ theme_void <- function(base_size = 11, base_family = "",
legend.text = element_text(size = rel(0.8)),
legend.title = element_text(hjust = 0),
strip.text = element_text(size = rel(0.8)),
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
strip.switch.pad.grid = unit(half_line / 2, "pt"),
strip.switch.pad.wrap = unit(half_line / 2, "pt"),
panel.ontop = FALSE,
panel.spacing = unit(half_line, "pt"),
plot.margin = unit(c(0, 0, 0, 0), "lines"),
plot.title = element_text(
size = rel(1.2),
hjust = 0, vjust = 1,
margin = margin(t = half_line * 1.2)
margin = margin(t = half_line)
),
plot.subtitle = element_text(
size = rel(0.9),
hjust = 0, vjust = 1,
margin = margin(t = half_line * 0.9)
margin = margin(t = half_line)
),
plot.caption = element_text(
size = rel(0.9),
size = rel(0.8),
hjust = 1, vjust = 1,
margin = margin(t = half_line * 0.9)
margin = margin(t = half_line)
),

complete = TRUE
Expand Down Expand Up @@ -480,26 +489,26 @@ theme_test <- function(base_size = 11, base_family = "",
axis.ticks = element_line(colour = "grey20"),
axis.ticks.length = unit(half_line / 2, "pt"),
axis.title.x = element_text(
margin = margin(t = half_line),
margin = margin(t = half_line / 2),
vjust = 1
),
axis.title.x.top = element_text(
margin = margin(b = half_line),
margin = margin(b = half_line / 2),
vjust = 0
),
axis.title.y = element_text(
angle = 90,
margin = margin(r = half_line),
margin = margin(r = half_line / 2),
vjust = 1
),
axis.title.y.right = element_text(
angle = -90,
margin = margin(l = half_line),
margin = margin(l = half_line / 2),
vjust = 0
),

legend.background = element_rect(colour = NA),
legend.spacing = unit(0.4, "cm"),
legend.spacing = unit(2 * half_line, "pt"),
legend.spacing.x = NULL,
legend.spacing.y = NULL,
legend.margin = margin(0, 0, 0, 0, "cm"),
Expand All @@ -517,7 +526,7 @@ theme_test <- function(base_size = 11, base_family = "",
legend.box = NULL,
legend.box.margin = margin(0, 0, 0, 0, "cm"),
legend.box.background = element_blank(),
legend.box.spacing = unit(0.4, "cm"),
legend.box.spacing = unit(2 * half_line, "pt"),

panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(fill = NA, colour = "grey20"),
Expand All @@ -532,31 +541,30 @@ theme_test <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = "grey10",
size = rel(0.8),
margin = margin(half_line, half_line, half_line, half_line)
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
),
strip.text.x = NULL,
strip.text.y = element_text(angle = -90),
strip.placement = "inside",
strip.placement.x = NULL,
strip.placement.y = NULL,
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
strip.switch.pad.grid = unit(half_line / 2, "pt"),
strip.switch.pad.wrap = unit(half_line / 2, "pt"),

plot.background = element_rect(colour = "white"),
plot.title = element_text(
size = rel(1.2),
hjust = 0, vjust = 1,
margin = margin(b = half_line * 1.2)
margin = margin(b = half_line)
),
plot.subtitle = element_text(
size = rel(0.9),
hjust = 0, vjust = 1,
margin = margin(b = half_line * 0.9)
margin = margin(b = half_line)
),
plot.caption = element_text(
size = rel(0.9),
size = rel(0.8),
hjust = 1, vjust = 1,
margin = margin(t = half_line * 0.9)
margin = margin(t = half_line)
),
plot.margin = margin(half_line, half_line, half_line, half_line),

Expand Down
Loading