Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created April 2, 2015 18:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 pictures such as this one that I've made - https://twitter.com/jalapic/status/583798245052284929/photo/1 - I manually made the legend (working on how to incorporate that). The title and footer are made using xlab and ggtitleparameters in the waffle function.

@jalapic
Copy link
Author

jalapic commented Apr 3, 2015

Additionally, I have updated this function so that an additional parameter picsize= is added. Simply this enables you to play around with the ysize=.4, bit such that the size of the phylopic can be changed around (will try to add to gist later).

@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