Monthly Archives: July 2013

LondonR lightning talk: Audiblization / sonification of data

It looks like the next LondonR meeting on 10 September 2013 will involve a series of 5 minute lightning talks rather than a few half hour slots. I have proposed “Audiblization / sonification of data: what are people doing and is R a good launchpad for it?”

Wot no subwoofer? Image courtesy of Matthew Grover.

This is at such an embryonic stage that it’s pretty comparable to graphs in the 1770s. There are even two terms in common usage, although it feels like consensus is moving towards ‘sonification’. We don’t even have a William Playfair yet, popularising it with good ideas. But some people are giving it a go and I’ll show a few examples. What I’m particularly interested in is using it as a communication tool for data and statistical patterns. There may be an exploratory role too but I’m unconvinced at present. R is well placed for this given the ease with which it can interface to other packages. Some people are keen on Python for much the same reason.┬áThere is at present a well-established R package tuneR which will handle some basic audio editing, and a package audio that does some I/O, but anything fancier will be best handled externally. There are, of course, powerful C++ libraries like CLAM, opening up the possibility of Rcpp-powered on-the-fly synthesis. With tuneR, for example, you work on a sound object and then end by writing it to the hard drive as a .wav file. Then, and only then, can you hear it.

Open-source synthesis packages like SuperCollider and PureData are powerful but essentially they speak a totally different language to stats people. It’s a bit like the old days where programming a GPU involved pretending your data was an image. We need data-oriented interfaces or this will remain the preserve of oddballs like me who have somehow acquired experience of stats and programming and┬ásound art or music. It will also remain hard to convince your boss that what you’re spending time on is not just messing about!

 

4 Comments

Filed under R

Courses on analysing electronic health record databases

Colleagues at University College London who work with the large primary care database THIN are running two short courses which would serve as an excellent (and rarely offered) introduction to this sort of work. Snap up places while you can:

  • Introduction to primary care databases, 11-13 November 2013
  • Missing data and new methods for multiple imputation of longitudinal electronic health records, 14-15 November 2013.

Leave a comment

Filed under Uncategorized

Easy pictograms using R

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

Edit 30 July 2013: Thank you to Paul Murrell, who wrote to tell me that you can do the same thing with vector images rather than raster using the grImport package and its function grid.symbols(). The advantage of vector images is that they don’t get pixellated and grainy as you zoom in on them.

Also, if you want to know more about how R handles raster images, you should check out Paul’s R Journal article from 2011.

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<allv) warning("Icons may extend above the top of the graph")
# vector of icons per row
iconrow<-as.vector(as.matrix(rbind(rep(hicons,length(vicons)),tail)))
# vector for how many times to repeat elements of iconrow
reprow<-as.vector(as.matrix(rbind((vicons-1),rep(1,length(vicons)))))
perrow<-untable(df=matrix(iconrow,ncol=1),num=reprow)
spacing<-NULL
for (i in 1:(length(n))) {
spacing<-c(spacing,rep((i-1)*vspace*ylength,n[i]))
}
y0<-spacing+(ylength*untable(df=matrix((1:allv)-1,ncol=1),num=perrow))
y1<-y0+all.ylengths
# there are more elegant ways to make x0, but for now...
x0<-NULL
for (i in 1:(length(perrow))) {
x0<-c(x0,(0:(perrow[i]-1)))
}
x1<-x0+xlength
leftplot<-floor(-(labprop*hicons))
plot(c(leftplot,hicons),c(0,(devaspect*hicons)),
type="n",bty="n",ylab="",xlab="",xaxt="n",yaxt="n")
lines(x=c(0,0),y=c(min(y0)-(ylength/2),max(y1)+(ylength/2)))
for (i in 1:sumn) {
rasterImage(image=licon[[group[i]]],xleft=x0[i],xright=x1[i],
ytop=y1[i],ybottom=y0[i])
}
# find positions for labels
ylabpos<-rep(NA,length(n))
for (i in 1:length(n)) {
ylabpos[i]<-(max(y1[group==i])+min(y0[group==i]))/2
}
text(x=leftplot/2,y=ylabpos,labels=grouplabels,cex=labelcex)
}

5 Comments

Filed under R

Interactive graphs are the new animated graphs

I just bought http://www.interactivegraphs.co.uk!

There’s nothing there just yet but the aim is, over this summer, to populate it as a sister site to http://www.animatedgraphs.co.uk, with tools that data-minded people can use within R, Stata, or even Excel, to turn data into online interactive stuff. This will all take the approach in R2leaflet: make a big chunk of text, containing your data, which can either be a standalone html file or a bit of JavaScript to include in a website. I’m pretty excited about it because it is going to provide a bridge from number-crunching to interactive graphics, and hopefully will introduce a lot of people to JavaScript along the way. I’ll keep you posted as it progresses…

Leave a comment

Filed under Uncategorized