From 21908b85b37f73ad7d27f43700e305b956123c96 Mon Sep 17 00:00:00 2001 From: William Gearty Date: Wed, 31 Jan 2024 16:30:11 -0500 Subject: [PATCH] Fix justification for add_phylopic_base and fix failing tests --- DESCRIPTION | 2 +- R/add_phylopic_base.r | 24 ++++++-------- R/geom_phylopic.R | 1 - .../phylopic-in-background.svg | 11 +++---- .../phylopic-key-glyph-with-img.svg | 21 ++++++------ .../phylopic-key-glyph-with-larger-glyphs.svg | 32 +++++++++---------- .../phylopic-key-glyph-with-uuid.svg | 21 ++++++------ .../geom_phylopic/phylopic-key-glyph.svg | 32 +++++++++---------- 8 files changed, 66 insertions(+), 78 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cebe844..1d3ed2d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,7 @@ Encoding: UTF-8 LazyData: true biocViews: Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Depends: R (>= 4.0) Imports: diff --git a/R/add_phylopic_base.r b/R/add_phylopic_base.r index e835794..51143c2 100644 --- a/R/add_phylopic_base.r +++ b/R/add_phylopic_base.r @@ -36,15 +36,9 @@ #' @param angle \code{numeric}. The number of degrees to rotate the silhouette #' clockwise. The default is no rotation. #' @param hjust \code{numeric}. A numeric vector between 0 and 1 specifying -#' horizontal justification (left = 0, center = 0.5, right = 1). Note that, -#' due to the enforcement of the silhouette's aspect ratio, there may be -#' unexpected behavior due to interactions between the aspect ratio of the -#' plot and the aspect ratio of the silhouette. +#' horizontal justification (left = 0, center = 0.5, right = 1). #' @param vjust \code{numeric}. A numeric vector between 0 and 1 specifying -#' vertical justification (top = 1, middle = 0.5, bottom = 0). Note that, due -#' to the enforcement of the silhouette's aspect ratio, there may be -#' unexpected behavior due to interactions between the aspect ratio of the -#' plot and the aspect ratio of the silhouette. +#' vertical justification (top = 1, middle = 0.5, bottom = 0). #' @param remove_background \code{logical}. Should any white background be #' removed from the silhouette(s)? See [recolor_phylopic()] for details. #' @param verbose \code{logical}. Should the attribution information for the @@ -122,10 +116,10 @@ add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL, if (any(alpha > 1 | alpha < 0)) { stop("`alpha` must be between 0 and 1.") } - if (any(data$hjust > 1 | data$hjust < 0)) { + if (any(hjust > 1 | hjust < 0)) { stop("`hjust` must be between 0 and 1.") } - if (any(data$vjust > 1 | data$vjust < 0)) { + if (any(vjust > 1 | vjust < 0)) { stop("`vjust` must be between 0 and 1.") } if (!is.logical(verbose)) { @@ -226,14 +220,16 @@ add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL, all(is.finite(img@summary@xscale)) && diff(img@summary@xscale) != 0 && is.numeric(img@summary@yscale) && length(img@summary@yscale) == 2 && all(is.finite(img@summary@yscale)) && diff(img@summary@yscale) != 0) { - grid.picture(img, x = x, y = y, height = ysize, expansion = 0, - just = c(hjust, vjust)) + xsize <- grconvertX( + grconvertY(ysize, from = "ndc", to = "inches") * aspect_ratio(img), + from = "inches", to = "ndc") + grid.picture(img, x = x, y = y, height = ysize, width = xsize, + expansion = 0, just = c(hjust, vjust)) } else { return(NULL) } } else { # png - grid.raster(img, x = x, y = y, height = ysize, - just = c(hjust, vjust)) + grid.raster(img, x = x, y = y, height = ysize, just = c(hjust, vjust)) } }, img = imgs, x = x, y = y, ysize = ysize, alpha = alpha, color = color, diff --git a/R/geom_phylopic.R b/R/geom_phylopic.R index f541997..1b8cae8 100644 --- a/R/geom_phylopic.R +++ b/R/geom_phylopic.R @@ -424,7 +424,6 @@ phylopicGrob <- function(img, x, y, height, color, fill, alpha, } } else { # png img_grob <- rasterGrob(img, x = x, y = y, height = height, - width = height * aspect_ratio(img), default.units = "native", just = c(hjust, vjust)) } diff --git a/tests/testthat/_snaps/add_phylopic_base/phylopic-in-background.svg b/tests/testthat/_snaps/add_phylopic_base/phylopic-in-background.svg index 97a8fc5..6676610 100644 --- a/tests/testthat/_snaps/add_phylopic_base/phylopic-in-background.svg +++ b/tests/testthat/_snaps/add_phylopic_base/phylopic-in-background.svg @@ -46,14 +46,13 @@ 1 - - + + - - - - + + + diff --git a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-img.svg b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-img.svg index f972aee..6246964 100644 --- a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-img.svg +++ b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-img.svg @@ -29,25 +29,24 @@ - - + + - - - - + + + - - + + - - - + + + diff --git a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-larger-glyphs.svg b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-larger-glyphs.svg index f3dfeb3..02f0d27 100644 --- a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-larger-glyphs.svg +++ b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-larger-glyphs.svg @@ -29,25 +29,24 @@ - - + + - - - - + + + - - + + - - - + + + @@ -72,14 +71,13 @@ name - - + + - - - - + + + diff --git a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-uuid.svg b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-uuid.svg index fc54e76..3442b14 100644 --- a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-uuid.svg +++ b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph-with-uuid.svg @@ -29,25 +29,24 @@ - - + + - - - - + + + - - + + - - - + + + diff --git a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph.svg b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph.svg index 5415915..1e64fdc 100644 --- a/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph.svg +++ b/tests/testthat/_snaps/geom_phylopic/phylopic-key-glyph.svg @@ -29,25 +29,24 @@ - - + + - - - - + + + - - + + - - - + + + @@ -72,14 +71,13 @@ name - - + + - - - - + + +