Skip to content

Commit 59130ef

Browse files
jihomnbram
authored andcommitted
Various fixes to themes (tidyverse#1679)
* Match default font size to new default Set in theme_grey to 11 * Match theme_bw with theme_grey better Keep contrast and size of gridlines (in particular, previously the minor grid lines were thicker than the major ones which looked weird) Remove the border around the legend key which complicates things visually for little gain Outline panel and facet strips with a dark-ish grey * Simplify theme_linedraw Use strip.text instead and strip.text.x and strip.text.y separately Slightly thicker grid lines compared to the previous iteration (but still quite thin) Base on theme_bw rather than on theme_grey * Improve and homogenise theme_dark and theme_light Use strip.text instead and strip.text.x and strip.text.y separately As in theme_grey and theme_bw, keep the same color for major and minor gridlines; just change the thickness (as was already done before). In theme_dark, make the strips the inverse of what they are in theme_grey * Reorder elements in the specification of theme_minimal Just to match theme_grey. No visual change beyond the changes to theme_bw on which this is based * Fix regressions in theme_classic Explicitly add axes which disappeared due to new specifications in theme_bw at some point. Increase size of the borders of strips to match axes visually. * Keep legend title in theme_void (otherwise the plot may be un-understandable) * Improve comments and reorder elements to match other theme specification * Fix alignment of code in all theme functions * Add NEWs bullet for theme update * Move theme_dark next to theme_light in the code They are related and their examples are in this order
1 parent 0498e48 commit 59130ef

File tree

3 files changed

+106
-99
lines changed

3 files changed

+106
-99
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
12
# ggplot2 2.1.0.9000
23

4+
* Themes are more homogeneous visually, and match `theme_grey` better.
5+
(@jiho, #1679)
6+
37
* `position_stack()` and `position_fill()` now sorts the stacking order so it
48
matches the order of the grouping. Use level reordering to alter the stacking
59
order. The default legend and stacking order is now also in line. The default

R/theme-defaults.r

Lines changed: 95 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -150,142 +150,145 @@ theme_gray <- theme_grey
150150

151151
#' @export
152152
#' @rdname ggtheme
153-
theme_bw <- function(base_size = 12, base_family = "") {
153+
theme_bw <- function(base_size = 11, base_family = "") {
154154
# Starts with theme_grey and then modify some parts
155155
theme_grey(base_size = base_size, base_family = base_family) %+replace%
156156
theme(
157-
axis.text = element_text(size = rel(0.8)),
158-
axis.ticks = element_line(colour = "black"),
159-
legend.key = element_rect(colour = "grey80"),
160-
panel.background = element_rect(fill = "white", colour = NA),
161-
panel.border = element_rect(fill = NA, colour = "grey50"),
162-
panel.grid.major = element_line(colour = "grey90", size = 0.2),
163-
panel.grid.minor = element_line(colour = "grey98", size = 0.5),
164-
strip.background = element_rect(fill = "grey80", colour = "grey50", size = 0.2)
157+
# white background and dark border
158+
panel.background = element_rect(fill = "white", colour = NA),
159+
panel.border = element_rect(fill = NA, colour = "grey20"),
160+
# make gridlines dark, same contrast with white as in theme_grey
161+
panel.grid.major = element_line(colour = "grey92"),
162+
panel.grid.minor = element_line(colour = "grey92", size = 0.25),
163+
# contour strips to match panel contour
164+
strip.background = element_rect(fill = "grey85", colour = "grey20"),
165+
# match legend key to background
166+
legend.key = element_rect(fill = "white", colour=NA)
165167
)
166168
}
167169

168170
#' @export
169171
#' @rdname ggtheme
170-
theme_linedraw <- function(base_size = 12, base_family = "") {
171-
half_line <- base_size / 2
172+
theme_linedraw <- function(base_size = 11, base_family = "") {
173+
# Starts with theme_bw and then modify some parts
174+
# = replace all greys with pure black or white
175+
theme_bw(base_size = base_size, base_family = base_family) %+replace%
176+
theme(
177+
# black text and ticks on the axes
178+
axis.text = element_text(colour = "black", size = rel(0.8)),
179+
axis.ticks = element_line(colour = "black", size = 0.25),
180+
# NB: match the *visual* thickness of axis ticks to the panel border
181+
# 0.5 clipped looks like 0.25
182+
183+
# pure black panel border and grid lines, but thinner
184+
panel.border = element_rect(fill = NA, colour = "black", size = 0.5),
185+
panel.grid.major = element_line(colour = "black", size = 0.05),
186+
panel.grid.minor = element_line(colour = "black", size = 0.025),
187+
188+
# strips with black background and white text
189+
strip.background = element_rect(fill = "black"),
190+
strip.text = element_text(colour = "white", size = rel(0.8))
191+
)
192+
}
193+
194+
#' @export
195+
#' @rdname ggtheme
196+
theme_light <- function(base_size = 11, base_family = "") {
172197
# Starts with theme_grey and then modify some parts
173198
theme_grey(base_size = base_size, base_family = base_family) %+replace%
174199
theme(
175-
axis.text = element_text(colour = "black", size = rel(0.8)),
176-
axis.ticks = element_line(colour = "black", size = 0.25),
177-
legend.key = element_rect(colour = "black", size = 0.25),
178-
panel.background = element_rect(fill = "white", colour = NA),
179-
panel.border = element_rect(fill = NA, colour = "black", size = 0.5),
180-
panel.grid.major = element_line(colour = "black", size = 0.05),
181-
panel.grid.minor = element_line(colour = "black", size = 0.01),
182-
strip.background = element_rect(fill = "black", colour = NA),
183-
strip.text.x = element_text(
184-
colour = "white",
185-
margin = margin(t = half_line, b = half_line)
186-
),
187-
strip.text.y = element_text(
188-
colour = "white",
189-
angle = 90,
190-
margin = margin(l = half_line, r = half_line)
191-
)
200+
# white panel with light grey border
201+
panel.background = element_rect(fill = "white", colour = NA),
202+
panel.border = element_rect(fill = NA, colour = "grey70", size = 0.5),
203+
# light grey, thinner gridlines
204+
# => make them slightly darker to keep acceptable contrast
205+
panel.grid.major = element_line(colour = "grey87", size = 0.25),
206+
panel.grid.minor = element_line(colour = "grey87", size = 0.125),
207+
208+
# match axes ticks thickness to gridlines and colour to panel border
209+
axis.ticks = element_line(colour = "grey70", size = 0.25),
210+
211+
# match legend key to panel.background
212+
legend.key = element_rect(fill = "white", colour = NA),
213+
214+
# dark strips with light text (inverse contrast compared to theme_grey)
215+
strip.background = element_rect(fill = "grey70", colour = NA),
216+
strip.text = element_text(colour = "white", size = rel(0.8))
192217
)
218+
193219
}
194220

195221
#' @export
196222
#' @rdname ggtheme
197-
theme_light <- function(base_size = 12, base_family = "") {
198-
half_line <- base_size / 2
223+
theme_dark <- function(base_size = 11, base_family = "") {
199224
# Starts with theme_grey and then modify some parts
200225
theme_grey(base_size = base_size, base_family = base_family) %+replace%
201226
theme(
202-
axis.ticks = element_line(colour = "grey70", size = 0.25),
203-
legend.key = element_rect(fill = "white", colour = "grey50", size = 0.25),
204-
panel.background = element_rect(fill = "white", colour = NA),
205-
panel.border = element_rect(fill = NA, colour = "grey70", size = 0.5),
206-
panel.grid.major = element_line(colour = "grey85", size = 0.25),
207-
panel.grid.minor = element_line(colour = "grey93", size = 0.125),
208-
strip.background = element_rect(fill = "grey70", colour = NA),
209-
strip.text.x = element_text(
210-
colour = "white",
211-
margin = margin(t = half_line, b = half_line)
212-
),
213-
strip.text.y = element_text(
214-
colour = "white",
215-
angle = -90,
216-
margin = margin(l = half_line, r = half_line)
217-
)
218-
)
227+
# dark panel
228+
panel.background = element_rect(fill = "grey50", colour = NA),
229+
# inverse grid lines contrast compared to theme_grey
230+
# make them thinner and try to keep the same visual contrast as in theme_light
231+
panel.grid.major = element_line(colour = "grey42", size = 0.25),
232+
panel.grid.minor = element_line(colour = "grey42", size = 0.125),
233+
234+
# match axes ticks thickness to gridlines
235+
axis.ticks = element_line(colour = "grey20", size = 0.25),
236+
237+
# match legend key to panel.background
238+
legend.key = element_rect(fill = "grey50", colour = NA),
219239

240+
# dark strips with light text (inverse contrast compared to theme_grey)
241+
strip.background = element_rect(fill = "grey15", colour = NA),
242+
strip.text = element_text(colour = "grey90", size = rel(0.8))
243+
)
220244
}
221245

222246
#' @export
223247
#' @rdname ggtheme
224-
theme_minimal <- function(base_size = 12, base_family = "") {
225-
# Starts with theme_bw and then modify some parts
248+
theme_minimal <- function(base_size = 11, base_family = "") {
249+
# Starts with theme_bw and remove most parts
226250
theme_bw(base_size = base_size, base_family = base_family) %+replace%
227251
theme(
252+
axis.ticks.x = element_blank(),
253+
axis.ticks.y = element_blank(),
228254
legend.background = element_blank(),
229255
legend.key = element_blank(),
230256
panel.background = element_blank(),
231257
panel.border = element_blank(),
232258
strip.background = element_blank(),
233-
plot.background = element_blank(),
234-
axis.ticks = element_line(),
235-
axis.ticks.x = element_blank(),
236-
axis.ticks.y = element_blank(),
237-
axis.ticks.length = unit(1, "lines")
259+
plot.background = element_blank()
238260
)
239261
}
240262

241263
#' @export
242264
#' @rdname ggtheme
243-
theme_classic <- function(base_size = 12, base_family = ""){
265+
theme_classic <- function(base_size = 11, base_family = ""){
244266
theme_bw(base_size = base_size, base_family = base_family) %+replace%
245267
theme(
268+
# no background and no grid
246269
panel.border = element_blank(),
247-
axis.line = element_line(colour = "black"),
248-
panel.grid.major = element_line(),
249-
panel.grid.major.x = element_blank(),
250-
panel.grid.major.y = element_blank(),
251-
panel.grid.minor = element_line(),
252-
panel.grid.minor.x = element_blank(),
253-
panel.grid.minor.y = element_blank(),
254-
strip.background = element_rect(colour = "black", size = 0.5),
255-
legend.key = element_blank()
256-
)
257-
}
270+
panel.grid.major = element_blank(),
271+
panel.grid.minor = element_blank(),
258272

259-
#' @export
260-
#' @rdname ggtheme
261-
theme_dark <- function(base_size = 12, base_family = "") {
262-
half_line <- base_size / 2
263-
# Starts with theme_grey and then modify some parts
264-
theme_grey(base_size = base_size, base_family = base_family) %+replace%
265-
theme(
266-
axis.ticks = element_line(colour = "grey40", size = 0.25),
267-
legend.key = element_rect(fill = "grey50", colour = "grey40", size = 0.25),
268-
panel.background = element_rect(fill = "grey50", colour = NA),
269-
panel.grid.major = element_line(colour = "grey40", size = 0.25),
270-
panel.grid.minor = element_line(colour = "grey45", size = 0.125),
271-
strip.background = element_rect(fill = "grey20", colour = NA),
272-
strip.text.x = element_text(
273-
colour = "white",
274-
margin = margin(t = half_line, b = half_line)
275-
),
276-
strip.text.y = element_text(
277-
colour = "white",
278-
angle = -90,
279-
margin = margin(l = half_line, r = half_line)
280-
)
273+
# show axes
274+
axis.line.x = element_line(colour = "black", size = 0.5),
275+
axis.line.y = element_line(colour = "black", size = 0.5),
276+
277+
# match legend key to panel.background
278+
legend.key = element_blank(),
279+
280+
# simple, black and white strips
281+
strip.background = element_rect(fill = "white", colour = "black", size = 1)
282+
# NB: size is 1 but clipped, it looks like the 0.5 of the axes
281283
)
282284
}
283285

284286
#' @export
285287
#' @rdname ggtheme
286-
theme_void <- function(base_size = 12, base_family = "") {
288+
theme_void <- function(base_size = 11, base_family = "") {
287289
theme(
288-
# Use only inherited elements and make everything blank
290+
# Use only inherited elements and make almost everything blank
291+
# Only keep indispensable text
289292
line = element_blank(),
290293
rect = element_blank(),
291294
text = element_text(
@@ -294,14 +297,14 @@ theme_void <- function(base_size = 12, base_family = "") {
294297
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
295298
margin = margin(), debug = FALSE
296299
),
297-
plot.margin = unit(c(0, 0, 0, 0), "lines"),
298300
axis.text.x = element_blank(),
299301
axis.text.y = element_blank(),
300302
axis.title.x = element_blank(),
301303
axis.title.y = element_blank(),
302304
legend.text = element_text(size = rel(0.8)),
303-
legend.title = element_blank(),
305+
legend.title = element_text(hjust = 0),
304306
strip.text = element_text(size = rel(0.8)),
307+
plot.margin = unit(c(0, 0, 0, 0), "lines"),
305308

306309
complete = TRUE
307310
)

man/ggtheme.Rd

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)