Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created April 2, 2015 18:12
Show Gist options
  • Save jalapic/13e97a888c98a372ff3b to your computer and use it in GitHub Desktop.
Save jalapic/13e97a888c98a372ff3b to your computer and use it in GitHub Desktop.
rphylopic waffle
#### Putting rphylopics into a waffle type chart
### WARNING - I know this code is a bit 'hacky' - I was just trying to make it work.
### There are likely better ways to do it, but this is how I got it up and running.
library(rphylopic)
#get phylopics
bird1<-get_image("23c63b97-e0ab-4b43-89a4-e2358f4f09ec", size="64")[[1]]
bird2<-get_image("abab8520-b72b-4ecb-97fd-945f8675fe61", size="64")[[1]]
bird3<-get_image("cab6e19e-00e9-401e-9b8a-c5ba70b5e9bb", size="64")[[1]]
phylowaffle <- function(x, colors, animals, rows=3){
library(waffle)
library(rphylopic)
obj <- waffle(x, rows, colors=rep("white", length(x)))
robj <- obj$data
mydf <- data.frame(value = levels(factor(robj$value)), animals)
mydf1 <- data.frame(value = levels(factor(robj$value)), colors)
robj$color <- mydf1$colors[match(robj$value, mydf1$value)] #add in animals
robj$animals <- mydf$animals[match(robj$value, mydf$value)] #add in animals
#loop to generate text
add<-NULL
for(i in 1:nrow(robj)){
add[[i]] <- paste0("add_phylopic(", robj[i,5], ", x=", robj[i,2], ", y=", robj[i,1], ", ysize=.4, alpha=1, color='", robj[i,4], "')")
}
add1 <- add[sapply(add, function(x) !grepl("NA",x))==T]
#loop to add to plot
for(i in 1:length(add1)){
obj <- obj + eval(parse(text=add1[i]))
}
return(obj)
}
###
phylowaffle(x=c(4,5,7),
animals=c("bird1", "bird2", "bird3"),
colors=c("black", "red", "mediumblue"),
rows=4)
@jalapic
Copy link
Author

jalapic commented Apr 3, 2015

For those that do not know much about phylopic - the images can be accessed here: http://phylopic.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment