Friday, March 21, 2014

Nice visualisation for single dimension data with labels

You may know Jari Oksanen et al's vegan (plant community ecology) package (http://cran.r-project.org/web/packages/vegan/vegan.pdf). It contains a neat function, 'linestack', for plotting labelled one-dimensional data so that the labels fit. I have found it a handy visualisation technique, e.g. for plotting correlations between plant taxa and an environmental variable (see real example plot below).



vectors <- data.frame(names=as.matrix(colors())[runif(40, min=1, max=650)], r = runif(40, min=-1, max=1))

vectors

                  names           r
1                orange -0.36946900
2                grey67 -0.15152158
3                  red2 -0.80901845
4        mediumseagreen  0.11225298
5            lightpink3  0.15080658
6                ivory1  0.11756504
7        cornflowerblue  0.57406034
8               oldlace -0.71916037
9        lightslategray -0.76570037
10               grey92  0.04404581
11       paleturquoise1  0.94855012
12               gray33  0.20623527
13                snow2 -0.31711523
14         navajowhite1 -0.53142940
15               gray71 -0.80816250
16               gray62 -0.13817008
17           steelblue3  0.57297053
18           lightcyan4  0.29911661
19 lightgoldenrodyellow -0.53221485
20               coral2 -0.66000856
21       paleturquoise2  0.83141393
22           palegreen4 -0.74607829
23                 pink  0.98511951
24               gray44  0.17423459
25              tomato1 -0.04841200
26          floralwhite  0.31414141
27               gray20 -0.02050105
28           lightcyan3  0.92788320
29               grey69 -0.84439991
30           burlywood3 -0.60867127
31                snow3  0.45804930
32               gray13  0.18198486
33               wheat2  0.71317735
34               purple -0.98669286
35              dimgrey -0.63993588
36               grey91  0.91495308
37        darkslateblue -0.54674852
38               gray14  0.59225932
39           indianred1  0.36252024
40            cadetblue  0.96656531



#plot - this code creates 2 side by side plots: the linestack on the left, and some descriptive arrows on the right

par(mfrow=c(1,2), mar=c(3,0,1,0))

linestack(vectors$r, labels=vectors$names, axis=TRUE, air=1.8, cex=0.7, hoff=7, font=6, at=-0.3)

plot(0.5, 0.5, ylim=c(-1,1),xlim=c(0,1), cex=0, xaxt="n", xlab="", bty="n", yaxt="n", ylab="")

arrows(0.1, 0.2, x1=0.1, y1=0.8, lwd=2, lty=1, angle=10)
arrows(0.1, -0.2, x1=0.1, y1=-0.8, lwd=2, angle=10)

text(0.05, -0.4, "negative", srt=90, cex=1.5)
text(0.05, 0.4, "positive", srt=90, cex=1.5)

Dummy example of 'linestack' in Vegan produced by above code.





No comments:

Post a Comment