Skip to content

Commit

Permalink
Unify inset_blanks() and inset_theme() help files.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Jun 24, 2024
1 parent 1d3afce commit 836660b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# ggmagnify 0.4.1

* Bugfix: `inset_theme()` was overridden in explicitly passed-in inset `plot` objects. Thanks
@XanderDes
* Bugfix: `inset_theme()` was overridden in explicitly passed-in inset `plot`
objects. Thanks @XanderDes.


# ggmagnify 0.4.0

Expand Down
66 changes: 34 additions & 32 deletions R/inset-theme.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@

#' Default elements to blank in the ggmagnify inset
#'
#' @param ... Character vector of extra elements to blank.
#' @inherit geom_magnify params
#'
#' @return A character vector.
#' @export
inset_blanks <- function (..., axes = c("", "x", "y", "xy")) {
res <- c("plot.title", "plot.subtitle", "plot.caption", "plot.tag",
"axis.title", "axis.title.x", "axis.title.y",
"strip.background", "strip.background.x", "strip.background.y",
"strip.text", "strip.text.x", "strip.text.y",
"strip.text.x.bottom", "strip.text.x.top",
"strip.text.y.left", "strip.text.y.right",
...)
axes <- rlang::arg_match(axes)
axis_suffixes_to_blank <- switch(paste0("+", axes), # avoid "zero-length name"
"+" = c("", ".x", ".y"),
"+x" = ".y",
"+y" = ".x",
"+xy" = character(0)
)
axis_bits <- c(outer(c("axis.text", "axis.ticks", "axis.line"),
axis_suffixes_to_blank, paste0))
res <- c(res, axis_bits)

res
}


#' Create a theme suitable for an inset ggplot
#'
#' Use `inset_theme()` to add a suitable theme to a manually-created inset
#' plot.
#' plot. Use `inset_blanks()` to customize the default list of elements to
#' blank in the inset.
#'
#' @param blank Character vector of theme elements to blank. See [ggplot2::theme()].
#' @param margin Margin around the plot. See `plot.margin` in [ggplot2::theme()].
#' @inherit geom_magnify params
#'
#' @return A ggplot theme object
#' @return `inset_theme()` returns a ggplot theme object. `inset_blanks()`
#' returns a character vector of theme element names.
#' @export
#' @doctest
#' library(ggplot2)
Expand All @@ -47,7 +20,8 @@ inset_blanks <- function (..., axes = c("", "x", "y", "xy")) {
#' from <- list(2.5, 3.5, 6, 7)
#' to <- list(4, 6, 5, 7)
#'
#' inset <- ggp + geom_density2d() + inset_theme(axes = "")
#' blanks <- inset_blanks("panel.grid")
#' inset <- ggp + geom_density2d() + inset_theme(axes = "", blank = blanks)
#' @expect silent()
#' ggp + geom_magnify(from = from, to = to, plot = inset)
inset_theme <- function (
Expand Down Expand Up @@ -76,3 +50,31 @@ inset_theme <- function (

thm
}


#' @param ... Character vector of extra elements to blank.
#' @inherit geom_magnify params
#'
#' @export
#' @rdname inset_theme
inset_blanks <- function (..., axes = c("", "x", "y", "xy")) {
res <- c("plot.title", "plot.subtitle", "plot.caption", "plot.tag",
"axis.title", "axis.title.x", "axis.title.y",
"strip.background", "strip.background.x", "strip.background.y",
"strip.text", "strip.text.x", "strip.text.y",
"strip.text.x.bottom", "strip.text.x.top",
"strip.text.y.left", "strip.text.y.right",
...)
axes <- rlang::arg_match(axes)
axis_suffixes_to_blank <- switch(paste0("+", axes), # avoid "zero-length name"
"+" = c("", ".x", ".y"),
"+x" = ".y",
"+y" = ".x",
"+xy" = character(0)
)
axis_bits <- c(outer(c("axis.text", "axis.ticks", "axis.line"),
axis_suffixes_to_blank, paste0))
res <- c(res, axis_bits)

res
}
20 changes: 0 additions & 20 deletions man/inset_blanks.Rd

This file was deleted.

16 changes: 12 additions & 4 deletions man/inset_theme.Rd

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

5 changes: 3 additions & 2 deletions tests/testthat/test-doctest-inset_theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
test_that("Doctest: inset_theme", {
# Created from @doctest for `inset_theme`
# Source file: R/inset-theme.R
# Source line: 43
# Source line: 16
library(ggplot2)
ggp <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + geom_point() + xlim(
c(2, 6))
from <- list(2.5, 3.5, 6, 7)
to <- list(4, 6, 5, 7)
inset <- ggp + geom_density2d() + inset_theme(axes = "")
blanks <- inset_blanks("panel.grid")
inset <- ggp + geom_density2d() + inset_theme(axes = "", blank = blanks)
expect_silent(ggp + geom_magnify(from = from, to = to, plot = inset))
})

0 comments on commit 836660b

Please sign in to comment.