Skip to content

Commit

Permalink
Independent placement of labels in coord_axes_inside() (#173)
Browse files Browse the repository at this point in the history
* allow separate x/y outside labels

* document

* add news bullet
  • Loading branch information
teunbrand authored Nov 16, 2024
1 parent bc67aa3 commit bf2a3f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Suggests:
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
Collate:
'at_panel.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggh4x (development version)

* `coord_axis_inside(labels_inside)` now supports independent `"x"` and `"y"`
(#167).
* Fixed bug in label remover (#158).
* Fixed bug in axis `check.overlap` setting (#165).
* Future-proofing of wrapped facets.
Expand Down
57 changes: 36 additions & 21 deletions R/coord_axes_inside.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ NULL
#' @param xintercept,yintercept A `numeric(1)` for the positions where the
#' orthogonal axes should be placed. If these are outside the bounds of the
#' limits, the axes are placed to the nearest extreme.
#' @param labels_inside A `logical(1)` when labels should be placed inside the
#' panel along the axes (`TRUE`) or placed outside the panel
#' (`FALSE`, default).
#' @param labels_inside One of `"x"`, `"y"`, `"both"` or `"none"` specifying
#' the axes where labels should be placed inside the panel along the axes.
#' `TRUE` is translated as `"both"` and `FALSE` (default) is translated as
#' `"none"`.
#' @param ratio Either `NULL`, or a `numeric(1)` for a fixed aspect ratio,
#' expressed as `y / x`.
#'
Expand Down Expand Up @@ -54,6 +55,13 @@ coord_axes_inside <- function(
clip = "on"
) {

if (is.character(labels_inside)) {
labels_inside <- arg_match0(labels_inside, c("x", "y", "none", "both"))
} else {
labels_inside <- if (isTRUE(labels_inside)) "both" else "none"
}

inner_axes <- theme()
outer_axes <- theme(
axis.line.x.bottom = element_blank(),
axis.line.x.top = element_blank(),
Expand All @@ -64,35 +72,42 @@ coord_axes_inside <- function(
axis.ticks.y.left = element_blank(),
axis.ticks.y.right = element_blank()
)
if (isTRUE(labels_inside)) {

if (labels_inside %in% c("x", "both")) {
outer_axes <- outer_axes + theme(
axis.text.x.bottom = element_blank(),
axis.text.x.top = element_blank(),
axis.text.y.left = element_blank(),
axis.text.y.right = element_blank(),
axis.text.x.bottom = element_blank(),
axis.text.x.top = element_blank(),
axis.ticks.length.x.bottom = unit(0, "pt"),
axis.ticks.length.x.top = unit(0, "pt"),
axis.ticks.length.y.left = unit(0, "pt"),
axis.ticks.length.y.right = unit(0, "pt")
)
inner_axes <- theme()
} else {
inner_axes <- theme(
inner_axes <- inner_axes + theme(
axis.text.x.bottom = element_blank(),
axis.text.x.top = element_blank(),
axis.text.y.left = element_blank(),
axis.text.y.right = element_blank()
axis.text.x.top = element_blank()
)
}
if (labels_inside %in% c("y", "both")) {
outer_axes <- outer_axes + theme(
axis.text.y.left = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.length.y.left = unit(0, "pt"),
axis.ticks.length.y.right = unit(0, "pt")
)
} else {
inner_axes <- inner_axes + theme(
axis.text.y.left = element_blank(),
axis.text.y.right = element_blank()
)
}

ggproto(
NULL, CoordAxesInside,
limits = list(x = xlim, y = ylim),
expand = expand,
default = default,
clip = clip,
ratio = ratio,
origin = data_frame0(x = xintercept[1], y = yintercept[1]),
limits = list(x = xlim, y = ylim),
expand = expand,
default = default,
clip = clip,
ratio = ratio,
origin = data_frame0(x = xintercept[1], y = yintercept[1]),
outer_axes = outer_axes,
inner_axes = inner_axes
)
Expand Down
7 changes: 4 additions & 3 deletions man/coord_axes_inside.Rd

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

0 comments on commit bf2a3f2

Please sign in to comment.