Skip to content

Commit c6623c3

Browse files
authored
Add base_line_size and base_rect_size parameters to theme defaults (#2190)
* Add base_line_size and base_rect_size parameters to theme defaults * Add tests for larger base_size values * Add line to NEWS.md * Set default base_(line|rect)_size in function definition
1 parent 314827a commit c6623c3

12 files changed

+770
-34
lines changed

NEWS.md

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

3+
* Theme functions now have the optional parameters `base_line_size` and
4+
`base_rect_size` to control the default sizes of line and rectangle elements
5+
(@karawoo, #2176).
6+
37
* Fixed bug in `coord_polar` that prevented secondary axis ticks and labels
48
from being drawn (@dylan-stark, #2072)
59

R/theme-defaults.r

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#'
77
#' @param base_size base font size
88
#' @param base_family base font family
9+
#' @param base_line_size base size for line elements
10+
#' @param base_rect_size base size for rect elements
911
#'
1012
#' @details
1113
#' \describe{
@@ -64,16 +66,22 @@ NULL
6466
#' @include theme.r
6567
#' @export
6668
#' @rdname ggtheme
67-
theme_grey <- function(base_size = 11, base_family = "") {
69+
theme_grey <- function(base_size = 11, base_family = "",
70+
base_line_size = base_size / 22,
71+
base_rect_size = base_size / 22) {
6872
half_line <- base_size / 2
6973

7074
theme(
7175
# Elements in this first block aren't used directly, but are inherited
7276
# by others
73-
line = element_line(colour = "black", size = 0.5, linetype = 1,
74-
lineend = "butt"),
75-
rect = element_rect(fill = "white", colour = "black",
76-
size = 0.5, linetype = 1),
77+
line = element_line(
78+
colour = "black", size = base_line_size,
79+
linetype = 1, lineend = "butt"
80+
),
81+
rect = element_rect(
82+
fill = "white", colour = "black",
83+
size = base_rect_size, linetype = 1
84+
),
7785
text = element_text(
7886
family = base_family, face = "plain",
7987
colour = "black", size = base_size,
@@ -177,9 +185,16 @@ theme_gray <- theme_grey
177185

178186
#' @export
179187
#' @rdname ggtheme
180-
theme_bw <- function(base_size = 11, base_family = "") {
188+
theme_bw <- function(base_size = 11, base_family = "",
189+
base_line_size = base_size / 22,
190+
base_rect_size = base_size / 22) {
181191
# Starts with theme_grey and then modify some parts
182-
theme_grey(base_size = base_size, base_family = base_family) %+replace%
192+
theme_grey(
193+
base_size = base_size,
194+
base_family = base_family,
195+
base_line_size = base_line_size,
196+
base_rect_size = base_rect_size
197+
) %+replace%
183198
theme(
184199
# white background and dark border
185200
panel.background = element_rect(fill = "white", colour = NA),
@@ -198,10 +213,17 @@ theme_bw <- function(base_size = 11, base_family = "") {
198213

199214
#' @export
200215
#' @rdname ggtheme
201-
theme_linedraw <- function(base_size = 11, base_family = "") {
216+
theme_linedraw <- function(base_size = 11, base_family = "",
217+
base_line_size = base_size / 22,
218+
base_rect_size = base_size / 22) {
202219
# Starts with theme_bw and then modify some parts
203220
# = replace all greys with pure black or white
204-
theme_bw(base_size = base_size, base_family = base_family) %+replace%
221+
theme_bw(
222+
base_size = base_size,
223+
base_family = base_family,
224+
base_line_size = base_line_size,
225+
base_rect_size = base_rect_size
226+
) %+replace%
205227
theme(
206228
# black text and ticks on the axes
207229
axis.text = element_text(colour = "black", size = rel(0.8)),
@@ -224,9 +246,16 @@ theme_linedraw <- function(base_size = 11, base_family = "") {
224246

225247
#' @export
226248
#' @rdname ggtheme
227-
theme_light <- function(base_size = 11, base_family = "") {
249+
theme_light <- function(base_size = 11, base_family = "",
250+
base_line_size = base_size / 22,
251+
base_rect_size = base_size / 22) {
228252
# Starts with theme_grey and then modify some parts
229-
theme_grey(base_size = base_size, base_family = base_family) %+replace%
253+
theme_grey(
254+
base_size = base_size,
255+
base_family = base_family,
256+
base_line_size = base_line_size,
257+
base_rect_size = base_rect_size
258+
) %+replace%
230259
theme(
231260
# white panel with light grey border
232261
panel.background = element_rect(fill = "white", colour = NA),
@@ -253,9 +282,16 @@ theme_light <- function(base_size = 11, base_family = "") {
253282

254283
#' @export
255284
#' @rdname ggtheme
256-
theme_dark <- function(base_size = 11, base_family = "") {
285+
theme_dark <- function(base_size = 11, base_family = "",
286+
base_line_size = base_size / 22,
287+
base_rect_size = base_size / 22) {
257288
# Starts with theme_grey and then modify some parts
258-
theme_grey(base_size = base_size, base_family = base_family) %+replace%
289+
theme_grey(
290+
base_size = base_size,
291+
base_family = base_family,
292+
base_line_size = base_line_size,
293+
base_rect_size = base_rect_size
294+
) %+replace%
259295
theme(
260296
# dark panel
261297
panel.background = element_rect(fill = "grey50", colour = NA),
@@ -280,9 +316,16 @@ theme_dark <- function(base_size = 11, base_family = "") {
280316

281317
#' @export
282318
#' @rdname ggtheme
283-
theme_minimal <- function(base_size = 11, base_family = "") {
319+
theme_minimal <- function(base_size = 11, base_family = "",
320+
base_line_size = base_size / 22,
321+
base_rect_size = base_size / 22) {
284322
# Starts with theme_bw and remove most parts
285-
theme_bw(base_size = base_size, base_family = base_family) %+replace%
323+
theme_bw(
324+
base_size = base_size,
325+
base_family = base_family,
326+
base_line_size = base_line_size,
327+
base_rect_size = base_rect_size
328+
) %+replace%
286329
theme(
287330
axis.ticks = element_blank(),
288331
legend.background = element_blank(),
@@ -298,8 +341,15 @@ theme_minimal <- function(base_size = 11, base_family = "") {
298341

299342
#' @export
300343
#' @rdname ggtheme
301-
theme_classic <- function(base_size = 11, base_family = ""){
302-
theme_bw(base_size = base_size, base_family = base_family) %+replace%
344+
theme_classic <- function(base_size = 11, base_family = "",
345+
base_line_size = base_size / 22,
346+
base_rect_size = base_size / 22) {
347+
theme_bw(
348+
base_size = base_size,
349+
base_family = base_family,
350+
base_line_size = base_line_size,
351+
base_rect_size = base_rect_size
352+
) %+replace%
303353
theme(
304354
# no background and no grid
305355
panel.border = element_blank(),
@@ -322,7 +372,9 @@ theme_classic <- function(base_size = 11, base_family = ""){
322372

323373
#' @export
324374
#' @rdname ggtheme
325-
theme_void <- function(base_size = 11, base_family = "") {
375+
theme_void <- function(base_size = 11, base_family = "",
376+
base_line_size = base_size / 22,
377+
base_rect_size = base_size / 22) {
326378
theme(
327379
# Use only inherited elements and make almost everything blank
328380
# Only keep indispensable text
@@ -347,14 +399,20 @@ theme_void <- function(base_size = 11, base_family = "") {
347399

348400
#' @export
349401
#' @rdname ggtheme
350-
theme_test <- function(base_size = 11, base_family = "") {
402+
theme_test <- function(base_size = 11, base_family = "",
403+
base_line_size = base_size / 22,
404+
base_rect_size = base_size / 22) {
351405
half_line <- base_size / 2
352-
406+
353407
theme(
354-
line = element_line(colour = "black", size = 0.5, linetype = 1,
355-
lineend = "butt"),
356-
rect = element_rect(fill = "white", colour = "black",
357-
size = 0.5, linetype = 1),
408+
line = element_line(
409+
colour = "black", size = base_line_size,
410+
linetype = 1, lineend = "butt"
411+
),
412+
rect = element_rect(
413+
fill = "white", colour = "black",
414+
size = base_rect_size, linetype = 1
415+
),
358416
text = element_text(
359417
family = base_family, face = "plain",
360418
colour = "black", size = base_size,

man/ggtheme.Rd

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

0 commit comments

Comments
 (0)