Monday, December 18, 2017

Holiday caRd 2017!

Here is this year's card, with best wishes from both of us at the EEB & Flow!

It gets a little harder every year to figure these out. R's plotting capabilities improve every year, but usually via specialized packages. I've tried more and more to use as few additional packages beyond base, and to produce a script that is hopefully compatible across platforms.
  • For best performance, users must install the 'deldir' package and the 'RCurl' package. This lets you download the necessary data file with as little effort as possible. 
  • If you have trouble accessing the file via the URL, you can just download the data file from Github directly, making sure to load the file into R using the hashed out code in Lines6-7.
Then to run, copy the full code (below), OR download the source file from github ,
OR, the easiest way, run this quick code directly:


#To run 2017 caRd quickly.
require(RCurl)
myscript <- getURL("https://raw.githubusercontent.com/cmtucker/EEB-FlowMisc/master/2017caRd_script.R", ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)
source(textConnection(myscript))
Full script here.
#################EEB & Flow 2017 caRd
#First choose how to get the data:
#IF you chose to download the data file already, make sure you have the local file pathway correctly here:
# csv file available at: https://github.com/cmtucker/EEB-FlowMisc/blob/master/2017caRd_data.csv
#myfile <- "2017caRd_data.csv"
#newImage <- read.csv(myfile, header=TRUE)
#OR, use RCurl package to download it directly from github
require(RCurl)
myfile <- getURL("https://raw.githubusercontent.com/cmtucker/EEB-FlowMisc/master/2017caRd_data.csv", ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)
newImage <- read.csv(textConnection(myfile), header=TRUE)
####################Run the rest of the script
#This library is necessary, you may need to install
#install.packages("deldir")
require(deldir)
###Start data processing
rw = c(1, 500, 1, 500)
#number of colours to cluster to
kColors <- 150
#Kmean cluster points
kMeans <- kmeans(newImage[, 3:5], centers = kColors)
kMeansColor <- rgb(kMeans$centers[kMeans$cluster, ])
#calculate tesselations (deldir library)
newtess <- deldir(newImage, rw=rw, sort=TRUE, dpl=list(ndx=NULL))
w <- tile.list(newtess)
###Start card plotting
#Make device with set size, works on multiple OS
dev.new <- function(width = 5, height = 5){
platform <- sessionInfo()$platform
if (grepl("linux",platform)) {
x11(width=width, height=height)
}else{
if (grepl("pc",platform)) {
windows(width=width, height=height)
}else{
if (grepl("w32",platform)) {
windows(width=width, height=height)
}else{
if (grepl("apple", platform)){
quartz(width=width, height=height)} }}}}
#Plotting starts
dev.new(6, 6)
par(bg="grey10", mai=c(0.1,0.1,0.1,0.1), oma=c(0.1,0.1,0.1,0.1))
#Empty
plot(x=newImage$x, y=newImage$y, col = rgb(newImage[3:5]), asp = 1, pch=".", xlim=c(rw[1:2]), ylim=rw[3:4], xaxt="n", yaxt="n", ylab="", xlab="", bty="n", type="n")
#View tesselations
plot(newtess, wlines="tess", wpoints="none", number=FALSE, lty=1, showrect=FALSE, xlim=c(min(cImage$x), max(cImage$x)), ylim=c(min(cImage$y), max(cImage$y)), bty="n", xaxt="n", yaxt="n", ylab="", xlab="", bty="n", add=TRUE)
#And fill
plot(w, fillcol= kMeansColor, close=FALSE, showpoints=FALSE, xlim=c(rw[1:2]), ylim=rw[3:4], add=TRUE)
text(x=375, y=475, "Happy Holidays from \n the EEB & Flow", col="white", cex=1.75, font=1)
#Fin
view raw caRd2017 hosted with ❤ by GitHub
(Bonus points for those who can guess which species of McArthur's warblers these are meant to be ;) )

No comments: