[R] x,y scatterplots, with averaging by a column

Jim Lemon jim at bitwrit.com.au
Mon Jun 30 12:53:36 CEST 2014

```On Mon, 30 Jun 2014 10:42:28 AM Tim Richter-Heitmann wrote:
> Hallo!
>
> I have this matrix:
>
> SampleID, Day, Species1, Species2,Species3,...,Speciesn
> 1,Monday,abundance values
> 2,Monday,abundance values
> 11,Tuesday,abundance values
> 12,Tuesday,abundance values
> 21,Wednesday,abundance values
> 22,Wednesday,abundance values
>
> I would like to plot the Days on the x-axis, and the species
abundance
> data on the y-axis. Each pair of values (i just have two
measurements
> for each day) should be represented by their mean with absolute
error
> bars (so we have basically a vertical min,max-range) and whiskers.
The
> means for each Species observation should be connected with lines.
> Also of interest would be annotating the whispers with their sample
ID
> (because the whiskers basically represent the values for y1,2
(11,12;
> 21,22)).

Hi Tim,
I think this might get you started:

trhdf<-data.frame(SampleID=seq(0,50,by=10)+(1:2),
Day=factor(rep( c("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"),
each=2),levels=c("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday")),
Species1=sample(0:20,12),Species2=sample(0:20,12),
Species3=sample(0:20,12),Species4=sample(0:20,12),
Species5=sample(0:20,12))

rollfunc<-function(x,FUN="mean",roll=2) {
lenx<-length(x)
xmeans<-rep(NA,lenx/2)
start<-1
for(xm in 1:(lenx/2)) {
xmeans[xm]<-do.call(FUN,list(x[start:(start+roll-1)]))
start<-start+roll
}
return(xmeans)
}

xpos<-rep(1:6,5)+seq(-0.4,0.4,by=0.2)
ypos<-as.vector(sapply(trhdf[,3:7],rollfunc))
ymin<-as.vector(sapply(trhdf[,3:7],rollfunc,"min"))
ymax<-as.vector(sapply(trhdf[,3:7],rollfunc,"max"))

library(plotrix)

plot(xpos,ypos,pch=c("1","2","3","4","5"),ylim=c(0,20),
xaxt="n",xlab="Day",ylab="Abundance")
dispersion(xpos,ypos,ymax,ymin,interval=FALSE)
staxlab(1,at=1:6,
labels=c("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"))

Jim

```