palaeoverse / rphylopic

Get Silhouettes of Organisms from PhyloPic
https://rphylopic.palaeoverse.org
GNU General Public License v3.0
91 stars 9 forks source link

Vectorize add_phylopic functions and add geom_phylopic #48

Closed willgearty closed 1 year ago

willgearty commented 1 year ago

This PR adds a new geom_phylopic() which acts like geom_point(), but places PhyloPic silhouettes instead. The silhouettes can be specified with "name", "uuid", or actual image objects (svg or png).

Note that the ggimage package also has a function called "geom_phylopic", but this function has several advantages:

I suppose we could call this something else, but I don't think there will be too much usage overlap.

In addition to the new function, I've also vectorized both add_phylopic (which uses ggplot2::annotate and the new GeomPhylopic) and add_phylopic_base (which now uses mapply to allow any argument to be vectorized).

Fixes https://github.com/palaeoverse-community/rphylopic/issues/25 and Fixes https://github.com/palaeoverse-community/rphylopic/issues/42.

willgearty commented 1 year ago

In creating this, I realized that the body of add_phylopic() could basically be changed to:

annotate(rphylopic:::GeomPhylopic, ...)

I think this would make it pretty easy to vectorize it for #42.

willgearty commented 1 year ago

Now supports polar coordinates (including the ggtree "circular" layout) as requested by @bschilder:

d <- data.table::fread("https://github.com/YuLab-SMU/ggtree/files/11001046/phylo_data.csv")
d <- as.data.frame(d) |> `rownames<-`(d$species)
tr <- rtree(nrow(d), tip.label = d$species)

ggtree(tr, layout = "circular") %<+% d  +
    geom_phylopic(aes(uuid=svg_uid), size = .1)

image

(I've reached out to PhyloPic about the silhouettes that are showing up as rectangles)

bschilder commented 1 year ago

Amazing! Thanks @willgearty !

Regarding the rectangle issue, this comes from the fact that some images have a filled background and others have a transparent background.

While the ultimate solution would be to have consistency within the phylopic database, @xiangpin came up with a fix using magick to remove the background on the fly: https://github.com/GuangchuangYu/ggimage/pull/46

willgearty commented 1 year ago

Thanks for pointing me to that PR @bschilder. Unfortunately, the default (and encouraged) functionality for rphylopic is to handle vector images (svgs), so magick::image_convert() won't work in this case. However, I have added the option to use the "original" color for now.

willgearty commented 1 year ago

Thanks for the thorough review @LewisAJones! I've fixed the handling of alpha values (now we just use recolor_phylopic instead of doing some funky/buggy stuff within grid.picture), fixed the handling of white backgrounds (https://github.com/keesey/phylopic/issues/14) which reduced the need for extra details about the color argument, and fixed a new bug related to highly nested svg objects (I think that may be my first recursive function that I've written in R).

Let me know what you think!