Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PhyloPic base R legend #99

Merged
merged 9 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(rotate_phylopic,Picture)
S3method(rotate_phylopic,array)
export(add_phylopic)
export(add_phylopic_base)
export(add_phylopic_legend)
export(browse_phylopic)
export(flip_phylopic)
export(geom_phylopic)
Expand Down Expand Up @@ -54,6 +55,7 @@ importFrom(grImport2,pictureGrob)
importFrom(grImport2,readPicture)
importFrom(graphics,grconvertX)
importFrom(graphics,grconvertY)
importFrom(graphics,legend)
importFrom(graphics,par)
importFrom(grid,gList)
importFrom(grid,gTree)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rphylopic (development version)

* Added add_phylopic_legend (#83)

# rphylopic 1.3.0

* updated citation
Expand Down
70 changes: 70 additions & 0 deletions R/add_phylopic_legend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Add a PhyloPic legend for a base R plot
#'
#' Specify existing images, taxonomic names, or PhyloPic uuids to add PhyloPic
#' silhouettes as a legend to an existing base R plot (like [legend()]).
#'
#' @param ysize \code{numeric}. Height of the silhouette. The width is
#' determined by the aspect ratio of the original image.
#' @inheritParams add_phylopic_base
#' @param ... Additional arguments passed to [legend()].
LewisAJones marked this conversation as resolved.
Show resolved Hide resolved
#'

Check warning on line 10 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=10,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @details This function can be used to add PhyloPic silhouettes as a
#' legend to a base R plot. Arguments available in [legend()] can be used and

Check warning on line 12 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=12,col=78,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' passed via `...`. Note that not all arguments in [legend()] are compatible
#' with [add_phylopic_legend()], such as `pch` and `lwd`. However, in general,
#' arguments for adjusting the legend appearance such as text (e.g. `cex`),

Check warning on line 15 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=15,col=76,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' legend box (e.g. `bg`), and color (e.g. `border`) are.
LewisAJones marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom graphics legend
#' @export
#' @examples
#' # Get UUIDs
#' uuids <- get_uuid(name = "Canis lupus", n = 2)
#' # Generate empty plot
#' plot(0:10, 0:10, type = "n", main = "Wolfs")
LewisAJones marked this conversation as resolved.
Show resolved Hide resolved
#' # Add data points
#' add_phylopic_base(uuid = uuids,
#' color = "black", fill = c("blue", "green"),
#' x = c(2.5, 7.5), y = c(2.5, 7.5), ysize = 2)
#' # Add legend
#' add_phylopic_legend(uuid = uuids,

Check warning on line 29 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=29,col=37,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' ysize = 0.5, color = "black", fill = c("blue", "green"),

Check warning on line 30 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=30,col=62,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' x = "bottomright", legend = c("Wolf 1", "Wolf 2"),
#' bg = "lightgrey")
add_phylopic_legend <- function(img = NULL, name = NULL, uuid = NULL,

Check warning on line 33 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=33,col=70,[trailing_whitespace_linter] Trailing whitespace is superfluous.
ysize = NULL, color = NA, fill = "black",

Check warning on line 34 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=34,col=74,[trailing_whitespace_linter] Trailing whitespace is superfluous.
...) {
# Get supplied arguments
args <- list(...)
# Filter unrequired arguments

Check warning on line 38 in R/add_phylopic_legend.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_legend.R,line=38,col=32,[trailing_whitespace_linter] Trailing whitespace is superfluous.
dump <- c("lty", "lwd", "pch", "angle", "density", "pt.lwd", "merge")
if (any(names(args) %in% dump)) {
args <- args[-which(names(args) %in% dump)]

Check warning on line 41 in R/add_phylopic_legend.R

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_legend.R#L41

Added line #L41 was not covered by tests
}
# Do call
leg_pos <- do.call(legend, args)
# Extract arguments if provided via legend for plotting
# color values
col <- args[["col"]]
if (!is.null(col)) color <- col

Check warning on line 48 in R/add_phylopic_legend.R

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_legend.R#L48

Added line #L48 was not covered by tests
border <- args[["border"]]
if (!is.null(border)) color <- border

Check warning on line 50 in R/add_phylopic_legend.R

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_legend.R#L50

Added line #L50 was not covered by tests
# fill value
bg <- args[["pt.bg"]]
if (!is.null(bg)) fill <- bg
# size values
size <- args[["pt.cex"]]
if (!is.null(size)) ysize <- size
# Set default ysize if required
if (is.null(ysize)) ysize <- (abs(diff(leg_pos$text$y)) * 0.5)
# Extract positions
# Adjust x position slightly to account for width
x <- leg_pos$text$x * 0.98
y <- leg_pos$text$y
LewisAJones marked this conversation as resolved.
Show resolved Hide resolved
# Plot
add_phylopic_base(uuid = uuid,
x = x,
y = y,
color = color,
fill = fill,
ysize = ysize)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ reference:
- phylopic_key_glyph
- add_phylopic
- add_phylopic_base
- add_phylopic_legend
- title: Modifying silhouettes
desc: >
Functions to modify silhouettes in various ways.
Expand Down
68 changes: 68 additions & 0 deletions man/add_phylopic_legend.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions tests/testthat/_snaps/add_phylopic_legend/phylopic-base-legend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions tests/testthat/test-add_phylopic_legend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_that("add_phylopic_legend works", {
skip_if_offline(host = "api.phylopic.org")
try(dev.off(), silent = TRUE) # clean up any stray plots

# Get UUIDs
uuids <- get_uuid(name = "Canis lupus", n = 2)

# PhyloPic base R legend
expect_doppelganger("PhyloPic base legend", function() {
plot(0:10, 0:10, type = "n", main = "Wolfs")
add_phylopic_base(uuid = uuids,
color = "black", fill = c("blue", "green"),
x = c(2.5, 7.5), y = c(2.5, 7.5), ysize = 2)
add_phylopic_legend(uuid = uuids,
ysize = 0.25, color = "black",
fill = c("blue", "green"),
x = "bottomright", legend = c("Wolf 1", "Wolf 2"),
bg = "lightgrey")
})

# PhyloPic base R legend with legend arguments
expect_doppelganger("PhyloPic base legend args", function() {
plot(0:10, 0:10, type = "n", main = "Wolfs")
add_phylopic_base(uuid = uuids,
color = "black", fill = c("blue", "green"),
x = c(2.5, 7.5), y = c(2.5, 7.5), ysize = 2)
add_phylopic_legend(uuid = uuids,
x = "bottomright", legend = c("Wolf 1", "Wolf 2"),
col = "black", pt.bg = c("blue", "green"),
pt.cex = 0.25)
})

# PhyloPic base R legend with default ysize
expect_doppelganger("PhyloPic base legend default size", function() {
plot(0:10, 0:10, type = "n", main = "Wolfs")
add_phylopic_base(uuid = uuids,
color = "black", fill = c("blue", "green"),
x = c(2.5, 7.5), y = c(2.5, 7.5), ysize = 2)
add_phylopic_legend(uuid = uuids,
x = "bottomright", legend = c("Wolf 1", "Wolf 2"),
col = "black", pt.bg = c("blue", "green"))
})
})
Loading