Skip to content

Commit

Permalink
Fix #27:
Browse files Browse the repository at this point in the history
- don't add `inset_theme` to a passed-in `plot` object
- add example to `inset_theme`
  • Loading branch information
hughjonesd committed Jun 23, 2024
1 parent f09c181 commit dbcea49
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Encoding: UTF-8
Language: en
Roxygen: list(markdown = TRUE, roclets = c("collate", "rd", "namespace",
"doctest::dt_roclet"), packages = "doctest")
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Depends:
R (>= 4.1.0)
Imports:
Expand Down
13 changes: 8 additions & 5 deletions R/geom-magnify.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ NULL
#' fill. Add alpha using e.g. [scales::alpha()]. Ignored when
#' `proj = "single"`.
#' @param plot Ggplot object to plot in the inset. If `NULL`, defaults to the
#' ggplot object to which `geom_magnify()` is added.
#' ggplot object to which `geom_magnify()` is added. Overrides `axes` if set.
#' Use [inset_theme()] to give `plot` an appropriate theme.
#' @param shadow.args List. Arguments to [ggfx::with_shadow()].
#' @param recompute Logical. If `TRUE`, use [lims()][ggplot2::lims()] to
#' replot the inset. Statistics, e.g. smoothing lines, will be
Expand Down Expand Up @@ -317,8 +318,11 @@ GeomMagnify <- ggproto("GeomMagnify", Geom,

# == create the magnified plot =======================================

plot <- plot %||% self$plot
plot_gtable <- create_plot_gtable(plot, data = d1, axes = axes,
if (is.null(plot)) {
plot <- self$plot
plot <- plot + inset_theme(axes = axes)
}
plot_gtable <- create_plot_gtable(plot, data = d1,
recompute = recompute,
scale.inset = scale.inset)

Expand Down Expand Up @@ -398,7 +402,7 @@ GeomMagnify <- ggproto("GeomMagnify", Geom,
)


create_plot_gtable <- function (plot, data, axes, recompute, scale.inset) {
create_plot_gtable <- function (plot, data, recompute, scale.inset) {
plot_coord <- ggplot_build(plot)$layout$coord
plot_limits <- plot_coord$limits

Expand Down Expand Up @@ -431,7 +435,6 @@ create_plot_gtable <- function (plot, data, axes, recompute, scale.inset) {
list(xlim = xlim_vals, ylim = ylim_vals, expand = FALSE))
}
)
plot <- plot + inset_theme(axes = axes)

suppressWarnings(suppressMessages({
plot_built <- ggplot_build(plot)
Expand Down
13 changes: 13 additions & 0 deletions R/inset-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@ inset_blanks <- function (..., axes) {

#' Create a theme suitable for an inset ggplot
#'
#' Use `inset_theme()` to add a suitable theme to a manually-created inset
#' plot.
#'
#' @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
#' @export
#' @doctest
#' 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 = "")
#' @expect silent()
#' ggp + geom_magnify(from = from, to = to, plot = inset)
inset_theme <- function (
blank = inset_blanks(axes = axes),
axes,
Expand Down
3 changes: 2 additions & 1 deletion man/geom_magnify.Rd

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

13 changes: 12 additions & 1 deletion man/inset_theme.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-doctest-geom_magnify.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
test_that("Doctest: geom_magnify", {
# Created from @doctest for `geom_magnify`
# Source file: R/geom-magnify.R
# Source line: 155
# Source line: 156
library(ggplot2)
ggp <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + geom_point() +
xlim(c(2, 6))
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)
expect_silent(ggp + geom_magnify(from = from, to = to))
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-doctest-inset_theme.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by doctest: do not edit by hand
# Please edit file in R/inset-theme.R

test_that("Doctest: inset_theme", {
# Created from @doctest for `inset_theme`
# Source file: R/inset-theme.R
# Source line: 43
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 = "")
expect_silent(ggp + geom_magnify(from = from, to = to, plot = inset))
})

2 changes: 1 addition & 1 deletion tests/testthat/test-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test_param("proj-linetype", proj.linetype = 1)
test_param("alpha", alpha = 0.5, colour = "red")
test_param("linewidth", linewidth = 1.5)
test_param("linetype", linetype = 2)
test_param("plot", plot = ggp + geom_density2d())
test_param("plot", plot = ggp + geom_density2d() + inset_theme(axes = ""))
test_param("recompute", recompute = TRUE, .ggplot = ggp + geom_density_2d())
test_param("scale-inset", scale.inset = c(2, 0.5))
test_param("shape-ellipse", shape = "ellipse")
Expand Down

0 comments on commit dbcea49

Please sign in to comment.