Skip to content

theme: Add different ticks length for different axes #2934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ This is a minor release and breaking changes have been kept to a minimum. End us
`nlevel`, an alias for `scaled`, to better match the syntax of `stat_bin()`
(@bjreisman, #2679).

* New theme elements allowing different ticks lengths for each
axis. For instance, this can be used to have inwards ticks on the
x-axis (`axis.ticks.length.x`) and outwards ticks on the y-axis
(`axis.ticks.length.y`). (@pank, #2935)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to nitpick, one last request, then I'll merge: Please move the period to after (@pank, #2935), just like in the other news bullets.


# ggplot2 3.0.0

Expand Down
49 changes: 37 additions & 12 deletions R/guides-axis.r
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ guide_axis <- function(at, labels, position = "right", theme) {
zero <- unit(0, "npc")
one <- unit(1, "npc")

theme$axis.ticks.length.x.bottom <- with(
theme,
axis.ticks.length.x.bottom %||%
axis.ticks.length.x %||%
axis.ticks.length
)
theme$axis.ticks.length.x.top <- with(
theme,
axis.ticks.length.x.top %||%
axis.ticks.length.x %||%
axis.ticks.length
)
theme$axis.ticks.length.y.left <- with(
theme,
axis.ticks.length.y.left %||%
axis.ticks.length.y %||%
axis.ticks.length
)
theme$axis.ticks.length.y.right <- with(
theme,
axis.ticks.length.y.right %||%
axis.ticks.length.y %||%
axis.ticks.length
)

label_render <- switch(position,
top = "axis.text.x.top", bottom = "axis.text.x.bottom",
left = "axis.text.y.left", right = "axis.text.y.right"
Expand All @@ -22,12 +47,12 @@ guide_axis <- function(at, labels, position = "right", theme) {
label_x <- switch(position,
top = ,
bottom = at,
right = theme$axis.ticks.length,
left = one - theme$axis.ticks.length
right = theme$axis.ticks.length.y.right,
left = one - theme$axis.ticks.length.y.left
)
label_y <- switch(position,
top = theme$axis.ticks.length,
bottom = one - theme$axis.ticks.length,
top = theme$axis.ticks.length.x.top,
bottom = one - theme$axis.ticks.length.x.bottom,
right = ,
left = at
)
Expand Down Expand Up @@ -58,18 +83,18 @@ guide_axis <- function(at, labels, position = "right", theme) {
ticks <- switch(position,
top = element_render(theme, "axis.ticks.x.top",
x = rep(at, each = 2),
y = rep(unit.c(zero, theme$axis.ticks.length), nticks),
y = rep(unit.c(zero, theme$axis.ticks.length.x.top), nticks),
id.lengths = rep(2, nticks)),
bottom = element_render(theme, "axis.ticks.x.bottom",
x = rep(at, each = 2),
y = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
y = rep(unit.c(one - theme$axis.ticks.length.x.bottom, one), nticks),
id.lengths = rep(2, nticks)),
right = element_render(theme, "axis.ticks.y.right",
x = rep(unit.c(zero, theme$axis.ticks.length), nticks),
x = rep(unit.c(zero, theme$axis.ticks.length.y.right), nticks),
y = rep(at, each = 2),
id.lengths = rep(2, nticks)),
left = element_render(theme, "axis.ticks.y.left",
x = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
x = rep(unit.c(one - theme$axis.ticks.length.y.left, one), nticks),
y = rep(at, each = 2),
id.lengths = rep(2, nticks))
)
Expand All @@ -79,21 +104,21 @@ guide_axis <- function(at, labels, position = "right", theme) {
top = gtable_col("axis",
grobs = list(labels, ticks),
width = one,
heights = unit.c(grobHeight(labels), theme$axis.ticks.length)
heights = unit.c(grobHeight(labels), theme$axis.ticks.length.x.top)
),
bottom = gtable_col("axis",
grobs = list(ticks, labels),
width = one,
heights = unit.c(theme$axis.ticks.length, grobHeight(labels))
heights = unit.c(theme$axis.ticks.length.x.bottom, grobHeight(labels))
),
right = gtable_row("axis",
grobs = list(ticks, labels),
widths = unit.c(theme$axis.ticks.length, grobWidth(labels)),
widths = unit.c(theme$axis.ticks.length.y.right, grobWidth(labels)),
height = one
),
left = gtable_row("axis",
grobs = list(labels, ticks),
widths = unit.c(grobWidth(labels), theme$axis.ticks.length),
widths = unit.c(grobWidth(labels), theme$axis.ticks.length.y.left),
height = one
)
)
Expand Down
18 changes: 18 additions & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ theme_grey <- function(base_size = 11, base_family = "",
axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line / 2), hjust = 0),
axis.ticks = element_line(colour = "grey20"),
axis.ticks.length = unit(half_line / 2, "pt"),
axis.ticks.length.x = NULL,
axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL,
axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL,
axis.ticks.length.y.right = NULL,
axis.title.x = element_text(
margin = margin(t = half_line / 2),
vjust = 1
Expand Down Expand Up @@ -459,6 +465,12 @@ theme_void <- function(base_size = 11, base_family = "",
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks.length = unit(0, "pt"),
axis.ticks.length.x = NULL,
axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL,
axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL,
axis.ticks.length.y.right = NULL,
legend.box = NULL,
legend.key.size = unit(1.2, "lines"),
legend.position = "right",
Expand Down Expand Up @@ -528,6 +540,12 @@ theme_test <- function(base_size = 11, base_family = "",
axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line / 2), hjust = 0),
axis.ticks = element_line(colour = "grey20"),
axis.ticks.length = unit(half_line / 2, "pt"),
axis.ticks.length.x = NULL,
axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL,
axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL,
axis.ticks.length.y.right = NULL,
axis.title.x = element_text(
margin = margin(t = half_line / 2),
vjust = 1
Expand Down
6 changes: 6 additions & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
axis.text.y.left = el_def("element_text", "axis.text.y"),
axis.text.y.right = el_def("element_text", "axis.text.y"),
axis.ticks.length = el_def("unit"),
axis.ticks.length.x = el_def("unit", "axis.ticks.length"),
axis.ticks.length.x.top = el_def("unit", "axis.ticks.length.x"),
axis.ticks.length.x.bottom = el_def("unit", "axis.ticks.length.x"),
axis.ticks.length.y = el_def("unit", "axis.ticks.length"),
axis.ticks.length.y.left = el_def("unit", "axis.ticks.length.y"),
axis.ticks.length.y.right = el_def("unit", "axis.ticks.length.y"),
axis.ticks.x = el_def("element_line", "axis.ticks"),
axis.ticks.x.top = el_def("element_line", "axis.ticks.x"),
axis.ticks.x.bottom = el_def("element_line", "axis.ticks.x"),
Expand Down
20 changes: 18 additions & 2 deletions R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
#' `axis.ticks.y.left`, `axis.ticks.y.right`). `axis.ticks.*.*` inherits from
#' `axis.ticks.*` which inherits from `axis.ticks`, which in turn inherits
#' from `line`
#' @param axis.ticks.length length of tick marks (`unit`)
#' @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
#' length of tick marks (`unit`)
#' @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
#' lines along axes ([element_line()]). Specify lines along all axes (`axis.line`),
#' lines for each plane (using `axis.line.x` or `axis.line.y`), or individually
Expand Down Expand Up @@ -192,12 +193,21 @@
#' )
#'
#' # Axes ----------------------------------------------------------------------
#' # Change styles of axes texts and lines
#' p1 + theme(axis.line = element_line(size = 3, colour = "grey80"))
#' p1 + theme(axis.text = element_text(colour = "blue"))
#' p1 + theme(axis.ticks = element_line(size = 2))
#' p1 + theme(axis.ticks.length = unit(.25, "cm"))
#'
#' # Change the appearance of the y-axis title
#' p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
#'
#' # Make ticks point outwards on y-axis and inwards on x-axis
#' p1 + theme(
#' axis.ticks.length.y = unit(.25, "cm"),
#' axis.ticks.length.x = unit(-.25, "cm"),
#' axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
#' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tidyverse indenting here (2 spaces only) and make sure there are two spaces around each =. Also, please add a comment explaining what this example shows (inward/outward pointing ticks on x/y axes).

#'
#' \donttest{
#' # Legend --------------------------------------------------------------------
#' p2 <- ggplot(mtcars, aes(wt, mpg)) +
Expand Down Expand Up @@ -275,6 +285,12 @@ theme <- function(line,
axis.ticks.y.left,
axis.ticks.y.right,
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,
axis.line,
axis.line.x,
axis.line.x.top,
Expand Down
10 changes: 8 additions & 2 deletions man/theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions tests/figs/themes/ticks_length.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading