Easy pictograms using R

[This article was first published on Robert Grant's stats blog » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I have been amazed for a while that there is no major stats software offering pictograms. You know the sort of classic infographic I mean:

Isotype’s classic design

Well, I have been working on an R function to help with this. It’s at Github here and below. Here’s an example:

man<-readPNG("man.png")

pictogram(icon=man,

n=c(12,35,52),

grouplabels=c("dudes","chaps","lads"))

same_icons

Simple, huh? You can also have more than one icon, although it’s up to you to keep them a sensible height:width ratio or ‘aspect’ to avoid distorting impressions.

pictogram(icon=list(man,holly,monster),

n=c(12,35,52),

grouplabels=c("men","holly","monsters"))

different_aspects

Suggestions? e-mail me or better still, pull them on Github. Happy pictogramming!

# requires image to be read in by readPNG or similar and supplied as "icon"

# To do: allow for non-integer n

pictogram<-function(icon,n,grouplabels="", hicons=20,vspace=0.5,labprop=0.2,labelcex=1) { if(is.list(icon)) { licon<-icon } else { licon<-list(icon) for (i in 2:length(n)) { licon[[i]]<-icon } } library(reshape) sumn<-sum(n) group<-untable(df=matrix((1:length(n)),ncol=1),num=n) vicons<-ceiling(n/hicons) allv<-sum(vicons) tail<-n%%hicons # dim[1] is the height, dim[2] the width: devaspect<-dev.size(units="px")[1]/dev.size(units="px")[2] xlength<-1 # get dims of all elements of licon, find greatest aspect and set ylength getdim<-function(z) { aspect<-dim(z)[1]/dim(z)[2] return(aspect) } all.ylengths<-unlist(lapply(licon,getdim)) ylength<-max(all.ylengths) all.ylengths<-untable(df=matrix(all.ylengths,ncol=1),num=n) ytop<-allv*ylength if(devaspect*hicons

To leave a comment for the author, please follow the link and comment on their blog: Robert Grant's stats blog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)