Skip to content

Commit 8359806

Browse files
pankclauswilke
authored andcommitted
theme: Add different ticks length for different axes (#2934)
* theme: Add different ticks length for different axes * guides-axis: make assignment a bit more elegant * styler changes to adhere to tidyverse style guide * Fix 456e742 -- that commit was too aggressive * NEWS: Update issue reference * Remove unintended changes to Rd files * theme: New example only uses one unit * theme-default: Update theme_void * Fix language and style according to Claus Wilke's recommendations * NEWS.md: Fix typo. * R/theme.r: Fix style and add comments for examples. * An additional language improvement suggested by Claus Wilke * Fix the NEWS.md entry * Move the location of the NEWS.md entry. Also use longer lines to somewhat better match other entries.
1 parent ce840ea commit 8359806

File tree

8 files changed

+183
-17
lines changed

8 files changed

+183
-17
lines changed

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ core developer team.
121121

122122
* `sec_axis()` now accepts functions as well as formulas (@yutannihilation, #3031).
123123

124+
* New theme elements allowing different ticks lengths for each axis. For instance,
125+
this can be used to have inwards ticks on the x-axis (`axis.ticks.length.x`) and
126+
outwards ticks on the y-axis (`axis.ticks.length.y`) (@pank, #2935).
127+
128+
124129
# ggplot2 3.1.0
125130

126131
## Breaking changes
@@ -230,7 +235,6 @@ This is a minor release and breaking changes have been kept to a minimum. End us
230235
`nlevel`, an alias for `scaled`, to better match the syntax of `stat_bin()`
231236
(@bjreisman, #2679).
232237

233-
234238
# ggplot2 3.0.0
235239

236240
## Breaking changes

R/guides-axis.r

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ guide_axis <- function(at, labels, position = "right", theme) {
1414
zero <- unit(0, "npc")
1515
one <- unit(1, "npc")
1616

17+
theme$axis.ticks.length.x.bottom <- with(
18+
theme,
19+
axis.ticks.length.x.bottom %||%
20+
axis.ticks.length.x %||%
21+
axis.ticks.length
22+
)
23+
theme$axis.ticks.length.x.top <- with(
24+
theme,
25+
axis.ticks.length.x.top %||%
26+
axis.ticks.length.x %||%
27+
axis.ticks.length
28+
)
29+
theme$axis.ticks.length.y.left <- with(
30+
theme,
31+
axis.ticks.length.y.left %||%
32+
axis.ticks.length.y %||%
33+
axis.ticks.length
34+
)
35+
theme$axis.ticks.length.y.right <- with(
36+
theme,
37+
axis.ticks.length.y.right %||%
38+
axis.ticks.length.y %||%
39+
axis.ticks.length
40+
)
41+
1742
label_render <- switch(position,
1843
top = "axis.text.x.top", bottom = "axis.text.x.bottom",
1944
left = "axis.text.y.left", right = "axis.text.y.right"
@@ -22,12 +47,12 @@ guide_axis <- function(at, labels, position = "right", theme) {
2247
label_x <- switch(position,
2348
top = ,
2449
bottom = at,
25-
right = theme$axis.ticks.length,
26-
left = one - theme$axis.ticks.length
50+
right = theme$axis.ticks.length.y.right,
51+
left = one - theme$axis.ticks.length.y.left
2752
)
2853
label_y <- switch(position,
29-
top = theme$axis.ticks.length,
30-
bottom = one - theme$axis.ticks.length,
54+
top = theme$axis.ticks.length.x.top,
55+
bottom = one - theme$axis.ticks.length.x.bottom,
3156
right = ,
3257
left = at
3358
)
@@ -58,18 +83,18 @@ guide_axis <- function(at, labels, position = "right", theme) {
5883
ticks <- switch(position,
5984
top = element_render(theme, "axis.ticks.x.top",
6085
x = rep(at, each = 2),
61-
y = rep(unit.c(zero, theme$axis.ticks.length), nticks),
86+
y = rep(unit.c(zero, theme$axis.ticks.length.x.top), nticks),
6287
id.lengths = rep(2, nticks)),
6388
bottom = element_render(theme, "axis.ticks.x.bottom",
6489
x = rep(at, each = 2),
65-
y = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
90+
y = rep(unit.c(one - theme$axis.ticks.length.x.bottom, one), nticks),
6691
id.lengths = rep(2, nticks)),
6792
right = element_render(theme, "axis.ticks.y.right",
68-
x = rep(unit.c(zero, theme$axis.ticks.length), nticks),
93+
x = rep(unit.c(zero, theme$axis.ticks.length.y.right), nticks),
6994
y = rep(at, each = 2),
7095
id.lengths = rep(2, nticks)),
7196
left = element_render(theme, "axis.ticks.y.left",
72-
x = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
97+
x = rep(unit.c(one - theme$axis.ticks.length.y.left, one), nticks),
7398
y = rep(at, each = 2),
7499
id.lengths = rep(2, nticks))
75100
)
@@ -79,21 +104,21 @@ guide_axis <- function(at, labels, position = "right", theme) {
79104
top = gtable_col("axis",
80105
grobs = list(labels, ticks),
81106
width = one,
82-
heights = unit.c(grobHeight(labels), theme$axis.ticks.length)
107+
heights = unit.c(grobHeight(labels), theme$axis.ticks.length.x.top)
83108
),
84109
bottom = gtable_col("axis",
85110
grobs = list(ticks, labels),
86111
width = one,
87-
heights = unit.c(theme$axis.ticks.length, grobHeight(labels))
112+
heights = unit.c(theme$axis.ticks.length.x.bottom, grobHeight(labels))
88113
),
89114
right = gtable_row("axis",
90115
grobs = list(ticks, labels),
91-
widths = unit.c(theme$axis.ticks.length, grobWidth(labels)),
116+
widths = unit.c(theme$axis.ticks.length.y.right, grobWidth(labels)),
92117
height = one
93118
),
94119
left = gtable_row("axis",
95120
grobs = list(labels, ticks),
96-
widths = unit.c(grobWidth(labels), theme$axis.ticks.length),
121+
widths = unit.c(grobWidth(labels), theme$axis.ticks.length.y.left),
97122
height = one
98123
)
99124
)

R/theme-defaults.r

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ theme_grey <- function(base_size = 11, base_family = "",
139139
axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line / 2), hjust = 0),
140140
axis.ticks = element_line(colour = "grey20"),
141141
axis.ticks.length = unit(half_line / 2, "pt"),
142+
axis.ticks.length.x = NULL,
143+
axis.ticks.length.x.top = NULL,
144+
axis.ticks.length.x.bottom = NULL,
145+
axis.ticks.length.y = NULL,
146+
axis.ticks.length.y.left = NULL,
147+
axis.ticks.length.y.right = NULL,
142148
axis.title.x = element_text(
143149
margin = margin(t = half_line / 2),
144150
vjust = 1
@@ -459,6 +465,12 @@ theme_void <- function(base_size = 11, base_family = "",
459465
axis.text = element_blank(),
460466
axis.title = element_blank(),
461467
axis.ticks.length = unit(0, "pt"),
468+
axis.ticks.length.x = NULL,
469+
axis.ticks.length.x.top = NULL,
470+
axis.ticks.length.x.bottom = NULL,
471+
axis.ticks.length.y = NULL,
472+
axis.ticks.length.y.left = NULL,
473+
axis.ticks.length.y.right = NULL,
462474
legend.box = NULL,
463475
legend.key.size = unit(1.2, "lines"),
464476
legend.position = "right",
@@ -528,6 +540,12 @@ theme_test <- function(base_size = 11, base_family = "",
528540
axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line / 2), hjust = 0),
529541
axis.ticks = element_line(colour = "grey20"),
530542
axis.ticks.length = unit(half_line / 2, "pt"),
543+
axis.ticks.length.x = NULL,
544+
axis.ticks.length.x.top = NULL,
545+
axis.ticks.length.x.bottom = NULL,
546+
axis.ticks.length.y = NULL,
547+
axis.ticks.length.y.left = NULL,
548+
axis.ticks.length.y.right = NULL,
531549
axis.title.x = element_text(
532550
margin = margin(t = half_line / 2),
533551
vjust = 1

R/theme-elements.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
292292
axis.text.y.left = el_def("element_text", "axis.text.y"),
293293
axis.text.y.right = el_def("element_text", "axis.text.y"),
294294
axis.ticks.length = el_def("unit"),
295+
axis.ticks.length.x = el_def("unit", "axis.ticks.length"),
296+
axis.ticks.length.x.top = el_def("unit", "axis.ticks.length.x"),
297+
axis.ticks.length.x.bottom = el_def("unit", "axis.ticks.length.x"),
298+
axis.ticks.length.y = el_def("unit", "axis.ticks.length"),
299+
axis.ticks.length.y.left = el_def("unit", "axis.ticks.length.y"),
300+
axis.ticks.length.y.right = el_def("unit", "axis.ticks.length.y"),
295301
axis.ticks.x = el_def("element_line", "axis.ticks"),
296302
axis.ticks.x.top = el_def("element_line", "axis.ticks.x"),
297303
axis.ticks.x.bottom = el_def("element_line", "axis.ticks.x"),

R/theme.r

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
#' `axis.ticks.y.left`, `axis.ticks.y.right`). `axis.ticks.*.*` inherits from
4848
#' `axis.ticks.*` which inherits from `axis.ticks`, which in turn inherits
4949
#' from `line`
50-
#' @param axis.ticks.length length of tick marks (`unit`)
50+
#' @param axis.ticks.length,axis.ticks.length.x,axis.ticks.length.x.top,axis.ticks.length.x.bottom,axis.ticks.length.y,axis.ticks.length.y.left,axis.ticks.length.y.right
51+
#' length of tick marks (`unit`)
5152
#' @param axis.line,axis.line.x,axis.line.x.top,axis.line.x.bottom,axis.line.y,axis.line.y.left,axis.line.y.right
5253
#' lines along axes ([element_line()]). Specify lines along all axes (`axis.line`),
5354
#' lines for each plane (using `axis.line.x` or `axis.line.y`), or individually
@@ -192,12 +193,21 @@
192193
#' )
193194
#'
194195
#' # Axes ----------------------------------------------------------------------
196+
#' # Change styles of axes texts and lines
195197
#' p1 + theme(axis.line = element_line(size = 3, colour = "grey80"))
196198
#' p1 + theme(axis.text = element_text(colour = "blue"))
197199
#' p1 + theme(axis.ticks = element_line(size = 2))
198-
#' p1 + theme(axis.ticks.length = unit(.25, "cm"))
200+
#'
201+
#' # Change the appearance of the y-axis title
199202
#' p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
200203
#'
204+
#' # Make ticks point outwards on y-axis and inwards on x-axis
205+
#' p1 + theme(
206+
#' axis.ticks.length.y = unit(.25, "cm"),
207+
#' axis.ticks.length.x = unit(-.25, "cm"),
208+
#' axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
209+
#' )
210+
#'
201211
#' \donttest{
202212
#' # Legend --------------------------------------------------------------------
203213
#' p2 <- ggplot(mtcars, aes(wt, mpg)) +
@@ -275,6 +285,12 @@ theme <- function(line,
275285
axis.ticks.y.left,
276286
axis.ticks.y.right,
277287
axis.ticks.length,
288+
axis.ticks.length.x,
289+
axis.ticks.length.x.top,
290+
axis.ticks.length.x.bottom,
291+
axis.ticks.length.y,
292+
axis.ticks.length.y.left,
293+
axis.ticks.length.y.right,
278294
axis.line,
279295
axis.line.x,
280296
axis.line.x.top,

man/theme.Rd

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

0 commit comments

Comments
 (0)