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?
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)
Does it answer your question ?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments