palaeoverse / rphylopic

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

rphylopic not working with ggdraw in cowplot #40

Closed ryanbstephens closed 1 year ago

ryanbstephens commented 1 year ago

Really love this package and so happy to see it up and working again! rphylopic used to work with ggdraw in cowplot but the latest version doesn't seem to be working. I'm hoping I'm just missing something in the new version.

library(ggplot2)
Plot <- ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Sepal.Width))

library(rphylopic)
Bird   <- get_phylopic("51ee5cb2-e7fe-48f5-939a-2aad427919ec", format = "512")

library(cowplot)
ggdraw() +
  draw_plot(Plot, x = 0, y = 0, width = 1, height = 1)+
  add_phylopic_base(img = Bird, x = 0.500, y = 0.95, ysize = 0.09, color = "blue", alpha = .9)# this used to work
willgearty commented 1 year ago

Hi @ryanbstephens, thanks for reaching out! Note that add_phylopic_base() should not be used in a "+" format. It is intended to be used as a standalone function (see the examples here). Let us know if that solves your issue.

Also, note that we strongly encourage users to use the default vector formats (format = "vector"). Finally, if you are already using ggplot, why not just use add_phylopic() as part of your ggplot workflow? Just wondering! (I understand you've likely simplified your example here for reproducibility)

willgearty commented 1 year ago

@ryanbstephens I just pushed some very minor changes to the github repo that should make your example work provided you switch to using add_phylopic(). Note that ggdraw() creates a new ggplot environment, so using add_phylopic_base() will not work, but add_phylopic() will (now). Also, note that if you use add_phylopic() as part of the original ggplot call, you will want to use x and y coordinates relative to the original plot extent, but if you use add_phylopic() as part of the ggdraw call, you will want to use x and y coordinates relative to the xlim and ylim you specify with ggdraw().

Let us know if this resolves your issue!

ryanbstephens commented 1 year ago

Hi @willgearty,

Thanks for the quick response and for making those changes. It works great now! Also, thanks for catching the “add_phylopic_base” typo in my first post. I was using “add_phylopic” but thought I would try the “base” just to make sure that wasn’t the issue and then forgot to change it back before posting. Also, good to know about using the default vector format.

Good question about using the silhouettes within the ggplot plot framework. I do that when I want to put a silhouette within the plot boundaries but sometimes it’s better to have the silhouette outside the plot boundaries (e.g., above facets) and cowplot is a nice way to do that after setting the height of the main plot to something less than 1.

Thanks again!

Ryan

willgearty commented 1 year ago

Ah, that all makes total sense, and I'm glad it works now! Please let us know if you come across any other issues or have any feature requests!

ryanbstephens commented 1 year ago

Will do. So far everything else works just like it did before plus I like the new features you added. I'm not sure how difficult it would be to implement, but it would be great to be able to rotate or flip an image, but I'm guessing that is a lot harder to implement. Thanks for maintaining this awesome package!

willgearty commented 1 year ago

@ryanbstephens you may want to check out the new arguments in the github version of add_phylopic() 😉 https://rphylopic.palaeoverse.org/reference/add_phylopic.html

ryanbstephens commented 1 year ago

Awesome! Thanks so much for adding this - it's a game changer for choosing images.