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 attribution option to plotting functions #79

Merged
merged 7 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* added preview argument to get_phylopic (#59)
* switched to {maps} package in base R advanced vignette
* added img argument to get_uuid and get_attribution
* added verbose argument (calls get_attribution) to geom_phylopic, add_phylopic, and add_phylopic_base (#71)

# rphylopic 1.1.1

Expand Down
6 changes: 4 additions & 2 deletions R/add_phylopic.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' clockwise. The default is no rotation.
#' @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
#' used silhouettes be printed to the console (see [get_attribution()])?
willgearty marked this conversation as resolved.
Show resolved Hide resolved
#' @details One (and only one) of `img`, `name`, or `uuid` must be specified.
#' Use parameters `x`, `y`, and `ysize` to place the silhouette at a specified
#' position on the plot. The aspect ratio of the silhouette will always be
Expand Down Expand Up @@ -72,7 +74,7 @@ add_phylopic <- function(img = NULL, name = NULL, uuid = NULL,
x, y, ysize = Inf,
alpha = 1, color = "black",
horizontal = FALSE, vertical = FALSE, angle = 0,
remove_background = TRUE) {
remove_background = TRUE, verbose = FALSE) {
LewisAJones marked this conversation as resolved.
Show resolved Hide resolved
if (all(sapply(list(img, name, uuid), is.null))) {
stop("One of `img`, `name`, or `uuid` is required.")
}
Expand All @@ -97,7 +99,7 @@ add_phylopic <- function(img = NULL, name = NULL, uuid = NULL,
args <- list(geom = GeomPhylopic, x = x, y = y, size = ysize,
alpha = alpha, color = color,
horizontal = horizontal, vertical = vertical, angle = angle,
remove_background = remove_background)
remove_background = remove_background, verbose = verbose)
# Only include the one silhouette argument
if (!is.null(img)) {
if (is.list(img)) {
Expand Down
18 changes: 14 additions & 4 deletions R/add_phylopic_base.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#' clockwise. The default is no rotation.
#' @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
#' used silhouettes be printed to the console (see [get_attribution()])?
willgearty marked this conversation as resolved.
Show resolved Hide resolved
#' @details One (and only one) of `img`, `name`, or `uuid` must be specified.
#' Use parameters `x`, `y`, and `ysize` to place the silhouette at a specified
#' position on the plot. If all three of these parameters are unspecified,
Expand Down Expand Up @@ -82,11 +84,11 @@
#' add_phylopic_base(img = cat, alpha = 0.2)
#' # overlay smaller cats
#' add_phylopic_base(img = cat, x = posx, y = posy, ysize = size, alpha = 0.8)
add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL,

Check warning on line 87 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=87,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 85.
x = NULL, y = NULL, ysize = NULL,
alpha = 1, color = "black",
horizontal = FALSE, vertical = FALSE, angle = 0,
remove_background = TRUE) {
remove_background = TRUE, verbose = FALSE) {
willgearty marked this conversation as resolved.
Show resolved Hide resolved
if (all(sapply(list(img, name, uuid), is.null))) {
stop("One of `img`, `name`, or `uuid` is required.")
}
Expand All @@ -101,17 +103,22 @@
if (!is.character(name)) {
stop("`name` should be of class character.")
}
if (!verbose) {
warning(paste("You've used the `name` argument. You may want to use",
willgearty marked this conversation as resolved.
Show resolved Hide resolved
"`verbose = TRUE` to get attribution information",
"for the silhouettes."), call. = FALSE)
willgearty marked this conversation as resolved.
Show resolved Hide resolved
}
# Get PhyloPic for each unique name
name_unique <- unique(name)
imgs <- sapply(name_unique, function(x) {
url <- tryCatch(get_uuid(name = x, url = TRUE),
id <- tryCatch(get_uuid(name = x),
error = function(cond) NA)

Check warning on line 115 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=115,col=22,[indentation_linter] Hanging indent should be 21 spaces but is 22 spaces.
if (is.na(url)) {
if (is.na(id)) {
warning(paste0("`name` ", '"', x, '"',
" returned no PhyloPic results."))
return(NULL)
}
get_svg(url)
get_phylopic(id)
})
imgs <- imgs[name]
} else if (!is.null(uuid)) {
Expand Down Expand Up @@ -139,6 +146,9 @@
}
imgs <- img
}
if (verbose) {
get_attribution(img = imgs, text = TRUE)
}

# get plot limits
usr <- par()$usr
Expand Down Expand Up @@ -169,7 +179,7 @@
# grobify and plot
if (is(img, "Picture")) { # svg
if ("summary" %in% slotNames(img) &&
all(c("xscale", "yscale") %in% slotNames(img@summary)) &&

Check warning on line 182 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=182,col=10,[indentation_linter] Indentation should be 12 spaces but is 10 spaces.
is.numeric(img@summary@xscale) && length(img@summary@xscale) == 2 &&
all(is.finite(img@summary@xscale)) && diff(img@summary@xscale) != 0 &&
is.numeric(img@summary@yscale) && length(img@summary@yscale) == 2 &&
Expand All @@ -182,5 +192,5 @@
grid.raster(img, x = x, y = y, height = ysize)
}
}, img = imgs, x = x, y = y, ysize = ysize, alpha = alpha, color = color,
horizontal = horizontal, vertical = vertical, angle = angle))

Check warning on line 195 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=195,col=5,[indentation_linter] Indentation should be 2 spaces but is 5 spaces.
}
32 changes: 24 additions & 8 deletions R/geom_phylopic.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
#' - vertical
#' - angle
#'
#' Learn more about setting these aesthetics in [add_phylopic()].
#' Learn more about setting these aesthetics in [add_phylopic()].
#'
#' @param show.legend logical. Should this layer be included in the legends?
#' `FALSE`, the default, never includes, `NA` includes if any aesthetics are
#' mapped, and `TRUE` always includes. It can also be a named logical vector
#' to finely select the aesthetics to display.
#' @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
#' used silhouettes be printed to the console (see [get_attribution()])?
willgearty marked this conversation as resolved.
Show resolved Hide resolved
#' @inheritParams ggplot2::layer
#' @inheritParams ggplot2::geom_point
#' @importFrom ggplot2 layer
Expand All @@ -61,13 +63,17 @@
geom_phylopic <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,

Check warning on line 66 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=66,col=27,[object_name_linter] Variable and function name style should match snake_case or symbols.
show.legend = FALSE,
inherit.aes = TRUE,
remove_background = TRUE) {
remove_background = TRUE,
verbose = FALSE) {
if (!is.logical(remove_background)) {
stop("`remove_background` should be a logical value.")
}
if (!is.logical(verbose)) {
stop("`verbose` should be a logical value.")
}
layer(
data = data,
mapping = mapping,
Expand All @@ -79,6 +85,7 @@
params = list(
na.rm = na.rm,
remove_background = remove_background,
verbose = verbose,
...
)
)
Expand All @@ -94,7 +101,7 @@
default_aes = aes(size = 1.5, alpha = 1, color = "black",
horizontal = FALSE, vertical = FALSE, angle = 0),
draw_panel = function(self, data, panel_params, coord, na.rm = FALSE,
remove_background = TRUE) {
remove_background = TRUE, verbose = FALSE) {
# Clean and transform data
data <- remove_missing(data, na.rm = na.rm, c("img", "name", "uuid"))
data <- coord$transform(data, panel_params)
Expand All @@ -113,20 +120,25 @@

# Check supplied data types and retrieve silhouettes if need be
if (cols["name"]) {
if (!verbose) {
warning(paste("You've used the `name` aesthetic/argument. You may want",
"to use `verbose = TRUE` to get attribution information",
"for the silhouettes."), call. = FALSE)
willgearty marked this conversation as resolved.
Show resolved Hide resolved
}
if (!is.character(data$name)) {
stop("The `name` aesthetic should be of class character.")
}
# Get PhyloPic for each unique name
name_unique <- unique(data$name)
imgs <- sapply(name_unique, function(name) {
url <- tryCatch(get_uuid(name = name, url = TRUE),
uuid <- tryCatch(get_uuid(name = name),
error = function(cond) NA)
if (is.na(url)) {
if (is.na(uuid)) {
warning(paste0("`name` ", '"', name, '"',
" returned no PhyloPic results."))
" returned no PhyloPic results."), call. = FALSE)
return(NULL)
}
get_svg(url)
get_phylopic(uuid)
})
imgs <- imgs[data$name]
} else if (cols["uuid"]) {
Expand All @@ -139,7 +151,8 @@
img <- tryCatch(get_phylopic(uuid),
error = function(cond) NULL)
if (is.null(img)) {
warning(paste0('"', uuid, '"', " is not a valid PhyloPic `uuid`."))
warning(paste0('"', uuid, '"', " is not a valid PhyloPic `uuid`."),
call. = FALSE)
}
img
})
Expand All @@ -153,6 +166,9 @@
}
imgs <- data$img
}
if (verbose) {
get_attribution(img = imgs, text = TRUE)
}

# Calculate height as percentage of y limits
# (or r limits for polar coordinates)
Expand Down
6 changes: 5 additions & 1 deletion man/add_phylopic.Rd

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

6 changes: 5 additions & 1 deletion man/add_phylopic_base.Rd

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

8 changes: 6 additions & 2 deletions man/geom_phylopic.Rd

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

Loading