File: get_axes.R

package info (click to toggle)
r-cran-cowplot 1.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,564 kB
  • sloc: sh: 13; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#' Get plot axes
#'
#' These functions extract just the axes from a ggplot. `get_y_axis()` pulls
#' the y-axis, while `get_x_axis()` pulls the x-axis.
#'
#' @param plot A ggplot or gtable.
#' @param position Which side of the plot is the axis on? For the x-axis, this
#'   can be "top" or "bottom", and for the y-axis, it can be "left" or "right".
#' @examples
#' library(ggplot2)
#'
#' p <- ggplot(mpg, aes(displ, cty)) +
#'   geom_point()
#'
#' ggdraw(get_y_axis(p))
#' p <- p + scale_x_continuous(position = "top")
#' ggdraw(get_x_axis(p, position = "top"))
#'
#' @export
get_y_axis <- function(plot, position = c("left", "right")) {
  position <- switch(match.arg(position), "left" = "l", "right" = "r")
  get_plot_component(plot, paste0("axis-", position))
}

#' @rdname get_y_axis
#' @export
get_x_axis <- function(plot, position = c("bottom", "top")) {
  position <- switch(match.arg(position), "bottom" = "b", "top" = "t")
  get_plot_component(plot, paste0("axis-", position))
}