How do I plot an image from phylopic in top right corner of my ggplot graph in R?

Kristen Cyr

I'm trying to import a silhouette of a torpedo ray into a ggplot bargraph. I want one image in the top right corner of each of my 3 facet wrapped graphs. When I try to plot it I get this error Error in img[, , 1] : incorrect number of dimensions and I have no idea how to fix it. Here is the code I'm using to plot it...

library(ggplot2)
library(rphylopic)
library(RCurl)
library(png)

#sample data
bargraph_dets_temp1 = data.frame(Zone = as.factor(sample(c(1,2,3,4), 22, replace = T)),
                                month = sample(month.abb, 22, replace = T),
                                year = sample(c(2016, 2017, 2018), 22, replace = T),
                                num_unique_tags = sample(c(1:9), 22, replace = T),
                                Season = sample(c("summer", "fall", "spring", "winter"), 22, replace =T))

#import phylopic
rayurl = "http://phylopic.org/assets/images/submissions/a3b3e80c-22f2-4b8f-a3ac-42fe1583e0be.thumb.png"

raylogo = readPNG(getURLContent(rayurl), native = T)


#Bargraph of ONLY the unique detections
ggplot(bargraph_dets_temp1, aes(x = Season)) +
  geom_bar(aes(fill = Zone, 
               y = bargraph_dets_temp1$num_unique_tags), 
           position = "dodge", stat = "identity") +
  facet_wrap(vars(year)) +
add_phylopic(raylogo)

Does anyone know how to add this silhouette into the graphs?

dc37

It seems that add_phylopic is taking only array objects. So, you can maybe find a way to transform your nativeraster object into an array.

Alternatively, you can use rasterGrob function from grid package to transform your image into a suitable format for ggplot and place it using annotation_custom:

library(grid)
raylogo = readPNG(getURLContent(rayurl), native = T)
ray2 <- rasterGrob(raylogo, interpolate=TRUE)

ggplot(bargraph_dets_temp1, aes(x = Season)) +
  geom_bar(aes(fill = Zone, 
               y = bargraph_dets_temp1$num_unique_tags), 
           position = "dodge", stat = "identity") +
  facet_wrap(vars(year)) +
  annotation_custom(ray2, xmin=3.5, xmax=4, ymin=7.5, ymax=Inf)

enter image description here

Does it answer your question ?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I align a div to the top right corner of my <img>?

How do I remove my name from the top right corner of Chrome?

How do I move hot corner thap opens Activities Overview from top left to top right corner?

How do I get my chip to appear on the top right corner of this app in react?

How can I put my searchbox in to the top right corner?

How can I overlay an icon on the top right corner of image?

How do I design a div with a top right corner icon?

How do I get my navigation bar to go across the entire page? Right now its in the top right corner

how to place button on top right corner of image

how do i check the number of 1 inside the top left corner to bottom right of my 5x5 matrix

How do i place a frame in the bottom right corner of my window?

How to keep an image in the top right corner of my webpage when zooming in/out?

How do I move ggplot strip labels so that there is one on top and one on the right and only label colums and rows instead of every plot

How can I change the digital clock position so it will be at the top right corner of my website?

How do I plot a graph by based on 3 groups in ggplot?

How do I remove the Centos 7 Desktop (Hot Corner) top right effect?

How do I make my link list appear in order from top to bottom, not right to left?

How to set the image icon on top right corner beyond the boundary of dialog

How do I make a floating image float to the top right of a div?

How do I plot on top of an existing image with a logarithmic axis?

How do I change the color of my bar plot using ggplot2 package of R?

How can i place cancel icon on top right corner

How to get the gradient color from top right to bottom left corner

React Native: How do I style my button to so it's in the bottom right corner of the screen and make it smaller?

How do I make the bottom area of my grid grow into the bottom right corner?

How do I stop my image from being offset to the right in wordpress?

Set a close icon in the top right corner of an image

Placing an image to the top right corner - CSS

Image in the top right corner of the table using css