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

Fix color/fill defaults and behavior #91

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,6 +2,7 @@

* updated citation
* added warning when specified size is more than 1000 times smaller than the y-axis range (mostly useful for when making maps with coord_sf) (#86)
* changed the defaults and behavior of the color and fill argument/aesthetics to better maintain backwards compatibility but also prevent unnecessary outlines (#87)

# rphylopic 1.2.2

Expand Down
14 changes: 8 additions & 6 deletions R/add_phylopic.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#' of the silhouette (0 is fully transparent, 1 is fully opaque).
#' @param color \code{character}. Color of silhouette outline. If "original" or
#' NA is specified, the original color of the silhouette outline will be used
#' (usually the same as "transparent").
#' (usually the same as "transparent"). To remove the outline, you can set
#' this to "transparent".
#' @param fill \code{character}. Color of silhouette. If "original" is
#' specified, the original color of the silhouette will be used (usually the
#' same as "black"). If `color` is specified and `fill` is NA the outline and
#' fill color will be the same.
#' same as "black"). If `color` is specified and `fill` is NA, `color` will be
#' used as the fill color (for backwards compatibility). To remove the fill,
#' you can set this to "transparent".
#' @param horizontal \code{logical}. Should the silhouette be flipped
#' horizontally?
#' @param vertical \code{logical}. Should the silhouette be flipped vertically?
Expand Down Expand Up @@ -68,20 +70,20 @@
#' angle <- runif(10, 0, 360)
#' hor <- sample(c(TRUE, FALSE), 10, TRUE)
#' ver <- sample(c(TRUE, FALSE), 10, TRUE)
#' cols <- sample(c("black", "darkorange", "grey42", "white"), 10,
#' fills <- sample(c("black", "darkorange", "grey42", "white"), 10,
#' replace = TRUE)
#' alpha <- runif(10, 0.3, 1)
#'
#' 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, ysize = sizey,
#' color = cols, alpha = alpha, angle = angle,
#' fill = fills, alpha = alpha, angle = angle,
#' horizontal = hor, vertical = ver)
#' p + ggtitle("R Cat Herd!!")
add_phylopic <- function(img = NULL, name = NULL, uuid = NULL, filter = NULL,
x, y, ysize = Inf,
alpha = 1, color = "black", fill = NA,
alpha = 1, color = NA, fill = "black",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be consistent with add_phylopic_base?

horizontal = FALSE, vertical = FALSE, angle = 0,
remove_background = TRUE, verbose = FALSE) {
if (all(sapply(list(img, name, uuid), is.null))) {
Expand Down
23 changes: 14 additions & 9 deletions R/add_phylopic_base.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#' of the silhouette (0 is fully transparent, 1 is fully opaque).
#' @param color \code{character}. Color of silhouette outline. If "original" or
#' NA is specified, the original color of the silhouette outline will be used
#' (usually the same as "transparent").
#' (usually the same as "transparent"). To remove the outline, you can set
#' this to "transparent".
#' @param fill \code{character}. Color of silhouette. If "original" is
#' specified, the original color of the silhouette will be used (usually the
#' same as "black"). If `color` is specified and `fill` is NA the outline and
#' fill color will be the same.
#' same as "black"). If `color` is specified and `fill` is NA, `color` will be
#' used as the fill color (for backwards compatibility). To remove the fill,
#' you can set this to "transparent".
#' @param horizontal \code{logical}. Should the silhouette be flipped
#' horizontally?
#' @param vertical \code{logical}. Should the silhouette be flipped vertically?
Expand Down Expand Up @@ -75,13 +77,13 @@
#' angle <- runif(10, 0, 360)
#' hor <- sample(c(TRUE, FALSE), 10, TRUE)
#' ver <- sample(c(TRUE, FALSE), 10, TRUE)
#' cols <- sample(c("black", "darkorange", "grey42", "white"), 10,
#' fills <- sample(c("black", "darkorange", "grey42", "white"), 10,
#' replace = TRUE)
#'
#' plot(posx, posy, type = "n", main = "A cat herd")
#' add_phylopic_base(uuid = "23cd6aa4-9587-4a2e-8e26-de42885004c9",
#' x = posx, y = posy, ysize = size,
#' color = cols, angle = angle,
#' fill = fills, angle = angle,
#' horizontal = hor, vertical = ver)
#'
#' # Example using a cat background
Expand All @@ -93,10 +95,10 @@
#' 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 98 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=98,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 91.
filter = NULL,
x = NULL, y = NULL, ysize = NULL,
alpha = 1, color = "black", fill = NA,
alpha = 1, color = NA, fill = "black",
horizontal = FALSE, vertical = FALSE, angle = 0,
remove_background = TRUE, verbose = FALSE) {
if (all(sapply(list(img, name, uuid), is.null))) {
Expand Down Expand Up @@ -190,15 +192,18 @@
if (angle != 0) img <- rotate_phylopic(img, angle)

# recolor if necessary
color <- if (is.na(color) || color == "original") NULL else color
if (is.na(fill)) fill <- color
fill <- if (fill == "original") NULL else fill
if (is.na(color) || color == "original") color <- NULL
if (is.na(fill)) {
fill <- color
color <- NULL

Check warning on line 198 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L197-L198

Added lines #L197 - L198 were not covered by tests
}
if (fill == "original") fill <- NULL

Check warning on line 200 in R/add_phylopic_base.r

View check run for this annotation

Codecov / codecov/patch

R/add_phylopic_base.r#L200

Added line #L200 was not covered by tests
img <- recolor_phylopic(img, alpha, color, fill, remove_background)

# 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 206 in R/add_phylopic_base.r

View workflow job for this annotation

GitHub Actions / lint

file=R/add_phylopic_base.r,line=206,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 Down
27 changes: 14 additions & 13 deletions R/geom_phylopic.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
#' specifies the height of the silhouettes in the units of the y axis. The
#' aspect ratio of the silhouettes will always be maintained.
#'
#' The `color` (default: "black"), `fill` (default: NA), and `alpha` (default:
#' 1) aesthetics can be used to change the outline color, fill color, and
#' transparency (outline and fill) of the silhouettes, respectively. If
#' `color` is specified and `fill` is NA the outline and fill color will be
#' the same. If "original" is specified for the `color` aesthetic, the
#' original color of the silhouette outline will be used (usually the same as
#' "transparent"). If "original" is specified for the `fill` aesthetic, the
#' original color of the silhouette body will be used (usually the same as
#' "black").
#' The `color` (default: NA), `fill` (default: "black"), and `alpha` (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is working as expected, e.g.

library(ggplot2)
df <- data.frame(x = c(2, 4), y = c(10, 20),
                 name = c("Felis silvestris catus", "Odobenus rosmarus"))
ggplot(df) +
  geom_phylopic(aes(x = x, y = y, name = name), color = NA, size = 10) +
  facet_wrap(~name) +
  coord_cartesian(xlim = c(1,6), ylim = c(5, 30))

image

#' default: 1) aesthetics can be used to change the outline color, fill color,
#' and transparency (outline and fill) of the silhouettes, respectively. If
#' `color` is specified and `fill` is NA `color` will be used as the fill
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I am not sure this is behaving as expected:

library(ggplot2)
df <- data.frame(x = c(2, 4), y = c(10, 20),
                 name = c("Felis silvestris catus", "Odobenus rosmarus"))
ggplot(df) +
  geom_phylopic(aes(x = x, y = y, name = name), fill = NA, color = "blue", size = 10) +
  facet_wrap(~name) +
  coord_cartesian(xlim = c(1,6), ylim = c(5, 30))

image

#' color (for backwards compatibility). If "original" is specified for the
#' `color` aesthetic, the original color of the silhouette outline will be
#' used (usually the same as "transparent"). If "original" is specified for
#' the `fill` aesthetic, the original color of the silhouette body will be
#' used (usually the same as "black"). To remove the fill or outline, you can
#' set `fill` or `color` to "transparent", respectively.
#'
#' The `horizontal` and `vertical` aesthetics can be used to flip the
#' silhouettes. The `angle` aesthetic can be used to rotate the silhouettes.
Expand Down Expand Up @@ -74,16 +75,16 @@
#' name = c("Felis silvestris catus", "Odobenus rosmarus"))
#' ggplot(df) +
#' geom_phylopic(aes(x = x, y = y, name = name),
#' color = "purple", size = 10) +
#' fill = "purple", size = 10) +
#' facet_wrap(~name) +
#' coord_cartesian(xlim = c(1,6), ylim = c(5, 30))
#' }
geom_phylopic <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,

Check warning on line 85 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=85,col=27,[object_name_linter] Variable and function name style should match snake_case or symbols.
show.legend = FALSE,

Check warning on line 86 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=86,col=27,[object_name_linter] Variable and function name style should match snake_case or symbols.
inherit.aes = TRUE,

Check warning on line 87 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=87,col=27,[object_name_linter] Variable and function name style should match snake_case or symbols.
remove_background = TRUE,
verbose = FALSE,
filter = NULL) {
Expand Down Expand Up @@ -118,13 +119,12 @@

#' @importFrom ggplot2 ggproto ggproto_parent Geom aes remove_missing
#' @importFrom grid gTree gList nullGrob
GeomPhylopic <- ggproto("GeomPhylopic", Geom,

Check warning on line 122 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=122,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 30.

Check warning on line 122 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=122,col=1,[object_name_linter] Variable and function name style should match snake_case or symbols.
required_aes = c("x", "y"),
non_missing_aes = c("size", "alpha", "color",
non_missing_aes = c("size", "alpha", "color", "fill",
"horizontal", "vertical", "angle"),
optional_aes = c("img", "name", "uuid"), # one and only one of these
default_aes = aes(size = 6, alpha = 1,
color = "black", fill = NA,
default_aes = aes(size = 6, alpha = 1, color = NA, fill = "black",
horizontal = FALSE, vertical = FALSE, angle = 0),
extra_params = c("na.rm", "remove_background", "verbose", "filter"),
setup_data = function(data, params) {
Expand Down Expand Up @@ -212,10 +212,11 @@
data <- ggproto_parent(Geom, self)$use_defaults(data, params, modifiers)
if (col_fill[1] && !col_fill[2]) {
data$fill <- data$colour
data$colour <- NA
}
data
},
draw_panel = function(self, data, panel_params, coord, na.rm = FALSE,

Check warning on line 219 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=219,col=58,[object_name_linter] Variable and function name style should match snake_case or symbols.
remove_background = TRUE, filter = NULL) {
phylopic_env$remove_background <- remove_background
# reset the legend key index (since the panel is plotted after the legend)
Expand Down Expand Up @@ -377,7 +378,7 @@
#' @importFrom grImport2 pictureGrob
#' @importFrom grid rasterGrob gList gTree nullGrob
#' @importFrom methods slotNames
phylopicGrob <- function(img, x, y, height, color, fill, alpha,

Check warning on line 381 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=381,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 70.

Check warning on line 381 in R/geom_phylopic.R

View workflow job for this annotation

GitHub Actions / lint

file=R/geom_phylopic.R,line=381,col=1,[object_name_linter] Variable and function name style should match snake_case or symbols.
horizontal, vertical, angle,
remove_background) {
# modified from add_phylopic for now
Expand Down
16 changes: 9 additions & 7 deletions man/add_phylopic.Rd

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

16 changes: 9 additions & 7 deletions man/add_phylopic_base.Rd

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

27 changes: 13 additions & 14 deletions man/geom_phylopic.Rd

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

30 changes: 15 additions & 15 deletions tests/testthat/_snaps/add_phylopic/phylopic-in-background.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading