Skip to content

Commit

Permalink
Address review; fix plotting with log axes
Browse files Browse the repository at this point in the history
  • Loading branch information
willgearty committed Aug 9, 2024
1 parent 55fd74d commit 20eb333
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 34 deletions.
4 changes: 2 additions & 2 deletions R/add_phylopic.r
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
#' # Put a silhouette in several places based on UUID
#' posx <- runif(10, 0, 10)
#' posy <- runif(10, 0, 10)
#' sizey <- runif(10, 0.4, 2)
#' heights <- runif(10, 0.4, 2)
#' angle <- runif(10, 0, 360)
#' hor <- sample(c(TRUE, FALSE), 10, TRUE)
#' ver <- sample(c(TRUE, FALSE), 10, TRUE)
Expand All @@ -97,7 +97,7 @@
#' p <- ggplot(data.frame(cat.x = posx, cat.y = posy), aes(cat.x, cat.y)) +
#' geom_blank() +
#' add_phylopic(uuid = "23cd6aa4-9587-4a2e-8e26-de42885004c9",
#' x = posx, y = posy, height = sizey,
#' x = posx, y = posy, height = heights,
#' fill = fills, alpha = alpha, angle = angle,
#' horizontal = hor, vertical = ver)
#' p + ggtitle("R Cat Herd!!")
Expand Down
61 changes: 44 additions & 17 deletions R/add_phylopic_base.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
#' Use "by" to limit results to images which do not require attribution, "nc"
#' for images which allows commercial usage, and "sa" for images without a
#' ShareAlike clause. The user can also combine these filters as a vector.
#' @param x \code{numeric}. x value of the silhouette center. Ignored if `y` and
#' `ysize` are not specified. If "NULL", the default, the mean value of the
#' x-axis is used.
#' @param y \code{numeric}. y value of the silhouette center. Ignored if `x` and
#' `ysize` are not specified. If "NULL", the default, the mean value of the
#' y-axis is used.
#' @param x \code{numeric}. x value of the silhouette center. If "NULL", the
#' default, the mean value of the x-axis is used.
#' @param y \code{numeric}. y value of the silhouette center. If "NULL", the
#' default, the mean value of the y-axis is used.
#' @param ysize `r lifecycle::badge("deprecated")` use the `height` or `width`
#' argument instead.
#' @param height \code{numeric}. Height of the silhouette in coordinate space.
Expand Down Expand Up @@ -55,12 +53,13 @@
#' @param verbose \code{logical}. Should the attribution information for the
#' used silhouette(s) be printed to the console (see [get_attribution()])?
#' @details One (and only one) of `img`, `name`, or `uuid` must be specified.
#' Use parameters `x`, `y`, and `height`/`width` to place the silhouette at a
#' specified position on the plot. If all of these parameters are unspecified,
#' then the silhouette will be plotted to the full height and/or width of the
#' plot. The aspect ratio of `Picture` objects will always be maintained (even
#' when a plot is resized). However, if the plot is resized after plotting a
#' silhouette, the absolute size and/or position of the silhouette may change.
#' Use parameters `x`, `y`, `hjust`, and `vjust` to place the silhouette at a
#' specified position on the plot. If `height` and `width` are both
#' unspecified, then the silhouette will be plotted to the full height and/or
#' width of the plot. The aspect ratio of `Picture` objects will always be
#' maintained (even when a plot is resized). However, if the plot is resized
#' after plotting a silhouette, the absolute size and/or position of the
#' silhouette may change.
#'
#' Any argument (except for `remove_background`) may be a vector of values if
#' multiple silhouettes should be plotted. In this case, all other arguments
Expand Down Expand Up @@ -205,11 +204,19 @@ add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL,
# get plot limits
usr <- par()$usr
usr_x <- if (par()$xlog) 10^usr[1:2] else usr[1:2]
#usr_x <- usr[1:2]

Check warning on line 207 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=207,col=4,[commented_code_linter] Commented code should be removed.
usr_y <- if (par()$ylog) 10^usr[3:4] else usr[3:4]
#usr_y <- usr[3:4]

Check warning on line 209 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=209,col=4,[commented_code_linter] Commented code should be removed.

# set default position and dimensions if need be
if (is.null(x)) x <- mean(usr_x)
if (is.null(y)) y <- mean(usr_y)
if (is.null(x)) {
mn <- mean(usr[1:2])
x <- if (par()$xlog) 10 ^ mn else mn
}
if (is.null(y)) {
mn <- mean(usr[3:4])
y <- if (par()$ylog) 10 ^ mn else mn
}
if (is.null(height) && is.null(width)) {
height <- abs(diff(usr_y))
width <- abs(diff(usr_x))
Expand All @@ -218,13 +225,33 @@ add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL,
# convert x and y to normalized device coordinates
x <- grconvertX(x, to = "ndc")
y <- grconvertY(y, to = "ndc")

Check warning on line 228 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=228,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# convert width and/or height to normalized device coordinates if need be
if (!is.null(height)) {
height <- grconvertY(height, to = "ndc") - grconvertY(0, to = "ndc")
if (any(height < (abs(diff(usr[3:4])) / 1000), na.rm = TRUE)) {
warning(paste("Your specified silhouette `height`(s) are more than",
"1000 times smaller than your y-axis range. You probably",
"want to use a larger `height`."), call. = FALSE)

Check warning on line 234 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L232-L234

Added lines #L232 - L234 were not covered by tests
}
if (par()$ylog) {
base_y <- grconvertY(1, to = "ndc")

Check warning on line 237 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L237

Added line #L237 was not covered by tests
} else {
base_y <- grconvertY(0, to = "ndc")
}
height <- grconvertY(height, to = "ndc") - base_y
}
if (!is.null(width)) {
width <- grconvertX(width, to = "ndc") - grconvertX(0, to = "ndc")
if (any(width < (abs(diff(usr[1:2])) / 1000), na.rm = TRUE)) {
warning(paste("Your specified silhouette `width`(s) are more than 1000",
"times smaller than your x-axis range. You probably want",
"to use a larger `width`."), call. = FALSE)

Check warning on line 247 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L245-L247

Added lines #L245 - L247 were not covered by tests
}
if (par()$xlog) {
base_x <- grconvertX(1, to = "ndc")

Check warning on line 250 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L250

Added line #L250 was not covered by tests
} else {
base_x <- grconvertX(0, to = "ndc")
}
width <- grconvertX(width, to = "ndc") - base_x
}

Check warning on line 256 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=256,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# change NULLs to NAs
Expand Down
4 changes: 2 additions & 2 deletions man/add_phylopic.Rd

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

23 changes: 11 additions & 12 deletions man/add_phylopic_base.Rd

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

2 changes: 1 addition & 1 deletion man/scales.Rd

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

0 comments on commit 20eb333

Please sign in to comment.