Skip to content

Commit 9c7dcde

Browse files
thomasp85karawoo
authored andcommitted
Add top and right theme settings to axis.ticks and axis.line (#2094)
* Add top and right theme settings to axis.ticks and axis.line * add left and bottom identifiers to all axis elements * Correctly reference the new bottom and left theme elements * Add visual test * Undo change to vdiffr version
1 parent 1f25e57 commit 9c7dcde

File tree

8 files changed

+237
-22
lines changed

8 files changed

+237
-22
lines changed

NEWS.md

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

3+
* Axes positioned on the top and to the right can now customize their ticks and
4+
lines separately (@thomasp85, #1899)
5+
36
* `geom_segment` now also takes a `linejoin` parameter. This allows more control over the appearance of the segments, which is especially useful for plotting thick arrows (@Ax3man, #774).
47

58
* Theme elements can now be subclassed. Add a `merge_element` method to control

R/guides-axis.r

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ guide_axis <- function(at, labels, position = "right", theme) {
1515
one <- unit(1, "npc")
1616

1717
label_render <- switch(position,
18-
top = "axis.text.x.top", bottom = "axis.text.x",
19-
left = "axis.text.y", right = "axis.text.y.right"
18+
top = "axis.text.x.top", bottom = "axis.text.x.bottom",
19+
left = "axis.text.y.left", right = "axis.text.y.right"
2020
)
2121

2222
label_x <- switch(position,
@@ -47,28 +47,28 @@ guide_axis <- function(at, labels, position = "right", theme) {
4747
left = element_render(theme, label_render, labels, y = label_y, expand_x = TRUE))
4848

4949
line <- switch(position,
50-
top = element_render(theme, "axis.line.x", c(0, 1), c(0, 0), id.lengths = 2),
51-
bottom = element_render(theme, "axis.line.x", c(0, 1), c(1, 1), id.lengths = 2),
52-
right = element_render(theme, "axis.line.y", c(0, 0), c(0, 1), id.lengths = 2),
53-
left = element_render(theme, "axis.line.y", c(1, 1), c(0, 1), id.lengths = 2)
50+
top = element_render(theme, "axis.line.x.top", c(0, 1), c(0, 0), id.lengths = 2),
51+
bottom = element_render(theme, "axis.line.x.bottom", c(0, 1), c(1, 1), id.lengths = 2),
52+
right = element_render(theme, "axis.line.y.right", c(0, 0), c(0, 1), id.lengths = 2),
53+
left = element_render(theme, "axis.line.y.left", c(1, 1), c(0, 1), id.lengths = 2)
5454
)
5555

5656
nticks <- length(at)
5757

5858
ticks <- switch(position,
59-
top = element_render(theme, "axis.ticks.x",
59+
top = element_render(theme, "axis.ticks.x.top",
6060
x = rep(at, each = 2),
6161
y = rep(unit.c(zero, theme$axis.ticks.length), nticks),
6262
id.lengths = rep(2, nticks)),
63-
bottom = element_render(theme, "axis.ticks.x",
63+
bottom = element_render(theme, "axis.ticks.x.bottom",
6464
x = rep(at, each = 2),
6565
y = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
6666
id.lengths = rep(2, nticks)),
67-
right = element_render(theme, "axis.ticks.y",
67+
right = element_render(theme, "axis.ticks.y.right",
6868
x = rep(unit.c(zero, theme$axis.ticks.length), nticks),
6969
y = rep(at, each = 2),
7070
id.lengths = rep(2, nticks)),
71-
left = element_render(theme, "axis.ticks.y",
71+
left = element_render(theme, "axis.ticks.y.left",
7272
x = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
7373
y = rep(at, each = 2),
7474
id.lengths = rep(2, nticks))

R/layout.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ Layout <- ggproto("Layout", NULL,
235235
render_labels = function(self, labels, theme) {
236236
label_grobs <- lapply(names(labels), function(label) {
237237
lapply(c(1, 2), function(i) {
238-
modify <- if (i == 2 && label == "y") ".right" else if (i == 1 && label == "x") ".top" else ""
238+
modify <- if (i == 1) {
239+
switch(label, x = ".top", y = ".left")
240+
} else {
241+
switch(label, x = ".bottom", y = ".right")
242+
}
239243
if (is.null(labels[[label]][[i]]) || is.waive(labels[[label]][[i]]))
240244
return(zeroGrob())
241245

R/theme-elements.r

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,29 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
276276
strip.text = el_def("element_text", "text"),
277277

278278
axis.line.x = el_def("element_line", "axis.line"),
279+
axis.line.x.top = el_def("element_line", "axis.line.x"),
280+
axis.line.x.bottom = el_def("element_line", "axis.line.x"),
279281
axis.line.y = el_def("element_line", "axis.line"),
282+
axis.line.y.left = el_def("element_line", "axis.line.y"),
283+
axis.line.y.right = el_def("element_line", "axis.line.y"),
280284
axis.text.x = el_def("element_text", "axis.text"),
281285
axis.text.x.top = el_def("element_text", "axis.text.x"),
286+
axis.text.x.bottom = el_def("element_text", "axis.text.x"),
282287
axis.text.y = el_def("element_text", "axis.text"),
288+
axis.text.y.left = el_def("element_text", "axis.text.y"),
283289
axis.text.y.right = el_def("element_text", "axis.text.y"),
284290
axis.ticks.length = el_def("unit"),
285291
axis.ticks.x = el_def("element_line", "axis.ticks"),
292+
axis.ticks.x.top = el_def("element_line", "axis.ticks.x"),
293+
axis.ticks.x.bottom = el_def("element_line", "axis.ticks.x"),
286294
axis.ticks.y = el_def("element_line", "axis.ticks"),
295+
axis.ticks.y.left = el_def("element_line", "axis.ticks.y"),
296+
axis.ticks.y.right = el_def("element_line", "axis.ticks.y"),
287297
axis.title.x = el_def("element_text", "axis.title"),
288298
axis.title.x.top = el_def("element_text", "axis.title.x"),
299+
axis.title.x.bottom = el_def("element_text", "axis.title.x"),
289300
axis.title.y = el_def("element_text", "axis.title"),
301+
axis.title.y.left = el_def("element_text", "axis.title.y"),
290302
axis.title.y.right = el_def("element_text", "axis.title.y"),
291303

292304
legend.background = el_def("element_rect", "rect"),

R/theme.r

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
#' `axis.title`)
2828
#' @param axis.title.x.top x axis label on top axis (`element_text`;
2929
#' inherits from `axis.title.x`)
30+
#' @param axis.title.x.bottom x axis label on bottom axis (`element_text`;
31+
#' inherits from `axis.title.x`)
3032
#' @param axis.title.y y axis label (`element_text`; inherits from
3133
#' `axis.title`)
34+
#' @param axis.title.y.left y axis label on left axis (`element_text`;
35+
#' inherits from `axis.title.y`)
3236
#' @param axis.title.y.right y axis label on right axis (`element_text`;
3337
#' inherits from `axis.title.y`)
3438
#' @param axis.text tick labels along axes (`element_text`; inherits from
@@ -37,23 +41,43 @@
3741
#' `axis.text`)
3842
#' @param axis.text.x.top x axis tick labels on top axis (`element_text`;
3943
#' inherits from `axis.text.x`)
44+
#' @param axis.text.x.bottom x axis tick labels on bottom axis (`element_text`;
45+
#' inherits from `axis.text.x`)
4046
#' @param axis.text.y y axis tick labels (`element_text`; inherits from
4147
#' `axis.text`)
48+
#' @param axis.text.y.left y axis tick labels on left axis
49+
#' (`element_text`; inherits from `axis.text.y`)
4250
#' @param axis.text.y.right y axis tick labels on right axis
4351
#' (`element_text`; inherits from `axis.text.y`)
4452
#' @param axis.ticks tick marks along axes (`element_line`; inherits from
4553
#' `line`)
4654
#' @param axis.ticks.x x axis tick marks (`element_line`; inherits from
4755
#' `axis.ticks`)
56+
#' @param axis.ticks.x.top x axis tick marks on top axis (`element_line`;
57+
#' inherits from `axis.ticks.x`)
58+
#' @param axis.ticks.x.bottom x axis tick marks on bottom axis (`element_line`;
59+
#' inherits from `axis.ticks.x`)
4860
#' @param axis.ticks.y y axis tick marks (`element_line`; inherits from
4961
#' `axis.ticks`)
62+
#' @param axis.ticks.y.left y axis tick marks on left axis (`element_line`;
63+
#' inherits from `axis.ticks.y`)
64+
#' @param axis.ticks.y.right y axis tick marks on right axis (`element_line`;
65+
#' inherits from `axis.ticks.y`)
5066
#' @param axis.ticks.length length of tick marks (`unit`)
5167
#' @param axis.line lines along axes (`element_line`; inherits from
5268
#' `line`)
5369
#' @param axis.line.x line along x axis (`element_line`; inherits from
5470
#' `axis.line`)
71+
#' @param axis.line.x.top line along x axis on top axis (`element_line`;
72+
#' inherits from `axis.line.x`)
73+
#' @param axis.line.x.bottom line along x axis on bottom axis (`element_line`;
74+
#' inherits from `axis.line.x`)
5575
#' @param axis.line.y line along y axis (`element_line`; inherits from
5676
#' `axis.line`)
77+
#' @param axis.line.y.left line along y axis on left axis (`element_line`;
78+
#' inherits from `axis.line.y`)
79+
#' @param axis.line.y.right line along y axis on right axis (`element_line`;
80+
#' inherits from `axis.line.y`)
5781
#'
5882
#' @param legend.background background of legend (`element_rect`; inherits
5983
#' from `rect`)
@@ -259,20 +283,32 @@ theme <- function(line,
259283
axis.title,
260284
axis.title.x,
261285
axis.title.x.top,
286+
axis.title.x.bottom,
262287
axis.title.y,
288+
axis.title.y.left,
263289
axis.title.y.right,
264290
axis.text,
265291
axis.text.x,
266292
axis.text.x.top,
293+
axis.text.x.bottom,
267294
axis.text.y,
295+
axis.text.y.left,
268296
axis.text.y.right,
269297
axis.ticks,
270298
axis.ticks.x,
299+
axis.ticks.x.top,
300+
axis.ticks.x.bottom,
271301
axis.ticks.y,
302+
axis.ticks.y.left,
303+
axis.ticks.y.right,
272304
axis.ticks.length,
273305
axis.line,
274306
axis.line.x,
307+
axis.line.x.top,
308+
axis.line.x.bottom,
275309
axis.line.y,
310+
axis.line.y.left,
311+
axis.line.y.right,
276312
legend.background,
277313
legend.margin,
278314
legend.spacing,

man/theme.Rd

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

0 commit comments

Comments
 (0)