1
votes

I'm trying to plot a boxplot for a time series (e.g. http://www.r-graph-gallery.com/146-boxplot-for-time-series/) and can get every other example to work, bar my last one. I have averages per month for six years (2011 to 2016) and have data for 2014 and 2015 (albeit in small quantities), but for some reason, boxes aren't being shown for the 2014 and 2015 data.

My input data has three columns: year, month and residency index (a value between 0 and 1). There are multiple individuals (in this example, 37) each with an average residency index per month per year (including 2014 and 2015).

For example:

year    month    RI
2015    1        NA
2015    2        NA
2015    3        NA
2015    4        NA
2015    5        NA
2015    6        NA
2015    7        0.387096774
2015    8        0.580645161
2015    9        0.3
2015    10       0.225806452
2015    11       0.3
2015    12       0.161290323
2016    1        0.096774194
2016    2        0.103448276
2016    3        0.161290323
2016    4        0.366666667
2016    5        0.258064516
2016    6        0.266666667
2016    7        0.387096774
2016    8        0.129032258
2016    9        0.133333333
2016    10       0.032258065
2016    11       0.133333333
2016    12       0.129032258

which is repeated for each individual fish.

My code:

   #make boxplot
   boxplot(RI$RI~RI$month+RI$year, 
   xaxt="n",xlab="",col=my_colours,pch=20,cex=0.3,ylab="Residency Index (RI)", ylim=c(0,1))
   abline(v=seq(0,12*6,12)+0.5,col="grey")
   axis(1,labels=unique(RI$year),at=seq(6,12*6,12))

The average trend line works as per the other examples.

   a=aggregate(RI$RI,by=list(RI$month,RI$year),mean, na.rm=TRUE)
   lines(a[,3],type="l",col="red",lwd=2)

Any help on this matter would be greatly appreciated.

1
Can't you post the data, or a subset of it?Rui Barradas
I've just added a small subset (very small) of my data. Hope you follow it.Rishard

1 Answers

0
votes

Your problem seems to be the presence of missing values, NA, in your data, the other values are plotted correctly. I've simplified your code a bit.

boxplot(RI$RI ~ RI$month + RI$year,
    ylab="Residency Index (RI)")
a <- aggregate(RI ~ month + year, data = RI, FUN = mean, na.rm = TRUE)
lines(c(rep(NA, 6), a[,3]), type="l", col="red", lwd=2)

Boxplot of Rishard's data

Also, I believe that maybe a boxplot is not the best way to depict your data. You only have one value per year/month, when a boxplot would require more. Maybe a simple scatter plot will do better.