Skip to content

Commit 1caf83f

Browse files
hadleymnbram
authored andcommitted
Add descenders to heights (tidyverse#1728)
* Add descenders to heights And increase margins slightly. Fixes tidyverse#1712 * Add issue number * Update theme test * Reduce outer margins
1 parent 59130ef commit 1caf83f

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

NEWS.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
21
# ggplot2 2.1.0.9000
32

3+
* When computing the height of titles ggplot2, now inclues the height of the
4+
descenders (i.e. the bits `g` and `y` that hang underneath). This makes
5+
improves the margins around titles, particularly the y axis label (#1712).
6+
7+
I have also very slightly increased the inner margins of axis titles,
8+
and removed the outer margins.
9+
410
* Themes are more homogeneous visually, and match `theme_grey` better.
511
(@jiho, #1679)
612

R/margins.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,26 @@ titleGrob <- function(label, x, y, hjust, vjust, angle = 0, gp = gpar(),
6262
text_grob <- textGrob(label, x, y, hjust = hjust, vjust = vjust,
6363
rot = angle, gp = gp)
6464

65+
# The grob dimensions don't include the text descenders, so add on using
66+
# a little trigonometry. This is only exactly correct when vjust = 1.
67+
descent <- descentDetails(text_grob)
68+
text_height <- unit(1, "grobheight", text_grob) + cos(angle / 180 * pi) * descent
69+
text_width <- unit(1, "grobwidth", text_grob) + sin(angle / 180 * pi) * descent
70+
6571
if (expand_x && expand_y) {
66-
widths <- unit.c(margin[4], unit(1, "grobwidth", text_grob), margin[2])
67-
heights <- unit.c(margin[1], unit(1, "grobheight", text_grob), margin[3])
72+
widths <- unit.c(margin[4], text_width, margin[2])
73+
heights <- unit.c(margin[1], text_height, margin[3])
6874

6975
vp <- viewport(layout = grid.layout(3, 3, heights = heights, widths = widths), gp = gp)
7076
child_vp <- viewport(layout.pos.row = 2, layout.pos.col = 2)
7177
} else if (expand_x) {
72-
widths <- unit.c(margin[4], unit(1, "grobwidth", text_grob), margin[2])
78+
widths <- unit.c(margin[4], text_width, margin[2])
7379
vp <- viewport(layout = grid.layout(1, 3, widths = widths), gp = gp)
7480
child_vp <- viewport(layout.pos.col = 2)
7581

7682
heights <- unit(1, "null")
7783
} else if (expand_y) {
78-
heights <- unit.c(margin[1], unit(1, "grobheight", text_grob), margin[3])
84+
heights <- unit.c(margin[1], text_height, margin[3])
7985

8086
vp <- viewport(layout = grid.layout(3, 1, heights = heights), gp = gp)
8187
child_vp <- viewport(layout.pos.row = 2)

R/theme-defaults.r

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ theme_grey <- function(base_size = 11, base_family = "") {
8585
axis.ticks = element_line(colour = "grey20"),
8686
axis.ticks.length = unit(half_line / 2, "pt"),
8787
axis.title.x = element_text(
88-
margin = margin(t = 0.8 * half_line, b = 0.8 * half_line / 2)
88+
margin = margin(t = half_line),
89+
vjust = 1
8990
),
9091
axis.title.y = element_text(
9192
angle = 90,
92-
margin = margin(r = 0.8 * half_line, l = 0.8 * half_line / 2)
93+
margin = margin(r = half_line),
94+
vjust = 1,
9395
),
9496

9597
legend.background = element_rect(colour = NA),
@@ -126,17 +128,17 @@ theme_grey <- function(base_size = 11, base_family = "") {
126128
plot.background = element_rect(colour = "white"),
127129
plot.title = element_text(
128130
size = rel(1.2),
129-
hjust = 0,
131+
hjust = 0, vjust = 1,
130132
margin = margin(b = half_line * 1.2)
131133
),
132134
plot.subtitle = element_text(
133135
size = rel(0.9),
134-
hjust = 0,
136+
hjust = 0, vjust = 1,
135137
margin = margin(b = half_line * 0.9)
136138
),
137139
plot.caption = element_text(
138140
size = rel(0.9),
139-
hjust = 1,
141+
hjust = 1, vjust = 1,
140142
margin = margin(t = half_line * 0.9)
141143
),
142144
plot.margin = margin(half_line, half_line, half_line, half_line),
@@ -179,12 +181,12 @@ theme_linedraw <- function(base_size = 11, base_family = "") {
179181
axis.ticks = element_line(colour = "black", size = 0.25),
180182
# NB: match the *visual* thickness of axis ticks to the panel border
181183
# 0.5 clipped looks like 0.25
182-
184+
183185
# pure black panel border and grid lines, but thinner
184186
panel.border = element_rect(fill = NA, colour = "black", size = 0.5),
185187
panel.grid.major = element_line(colour = "black", size = 0.05),
186188
panel.grid.minor = element_line(colour = "black", size = 0.025),
187-
189+
188190
# strips with black background and white text
189191
strip.background = element_rect(fill = "black"),
190192
strip.text = element_text(colour = "white", size = rel(0.8))
@@ -210,7 +212,7 @@ theme_light <- function(base_size = 11, base_family = "") {
210212

211213
# match legend key to panel.background
212214
legend.key = element_rect(fill = "white", colour = NA),
213-
215+
214216
# dark strips with light text (inverse contrast compared to theme_grey)
215217
strip.background = element_rect(fill = "grey70", colour = NA),
216218
strip.text = element_text(colour = "white", size = rel(0.8))
@@ -233,7 +235,7 @@ theme_dark <- function(base_size = 11, base_family = "") {
233235

234236
# match axes ticks thickness to gridlines
235237
axis.ticks = element_line(colour = "grey20", size = 0.25),
236-
238+
237239
# match legend key to panel.background
238240
legend.key = element_rect(fill = "grey50", colour = NA),
239241

tests/testthat/test-theme.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test_that("Modifying theme element properties with + operator", {
44

55
# Changing a "leaf node" works
66
t <- theme_grey() + theme(axis.title.x = element_text(colour = 'red', margin = margin()))
7-
expect_identical(t$axis.title.x, element_text(colour = 'red', margin = margin()))
7+
expect_identical(t$axis.title.x, element_text(colour = 'red', margin = margin(), vjust = 1))
88
# Make sure the theme class didn't change or get dropped
99
expect_true(is.theme(t))
1010
# Make sure the element class didn't change or get dropped

0 commit comments

Comments
 (0)