I would like to create a multivariate boxplot time serie with ggplot2 and i need to have an x axis with boxplot position function of dates.
I created for that an interaction matrix with the combination of the factors Treatment x Date
that is plotted against NDVI and with different trial groups:
here you can find some minimal data :
dat<-"Treatment Trial.group Date NDVI
HighN A 14/06/2013 0.27522123
HighN A 14/06/2013 0.259781926
HighN A 14/06/2013 0.175982276
LowN A 14/06/2013 0.193604644
LowN A 14/06/2013 0.261191793
LowN A 14/06/2013 0.273672853
HighN B 14/06/2013 0.192144884
HighN B 14/06/2013 0.283013594
HighN B 14/06/2013 0.230556973
LowN B 14/06/2013 0.233952974
LowN B 14/06/2013 0.261718465
LowN B 14/06/2013 0.216450145
HighN A 22/06/2013 0.37522123
HighN A 22/06/2013 0.359781926
HighN A 22/06/2013 0.275982276
LowN A 22/06/2013 0.293604644
LowN A 22/06/2013 0.361191793
LowN A 22/06/2013 0.373672853
HighN B 22/06/2013 0.292144884
HighN B 22/06/2013 0.383013594
HighN B 22/06/2013 0.330556973
LowN B 22/06/2013 0.333952974
LowN B 22/06/2013 0.361718465
LowN B 22/06/2013 0.316450145
HighN A 24/06/2013 0.47522123
HighN A 24/06/2013 0.459781926
HighN A 24/06/2013 0.375982276
LowN A 24/06/2013 0.393604644
LowN A 24/06/2013 0.461191793
LowN A 24/06/2013 0.473672853
HighN B 24/06/2013 0.392144884
HighN B 24/06/2013 0.483013594
HighN B 24/06/2013 0.430556973
LowN B 24/06/2013 0.433952974
LowN B 24/06/2013 0.461718465
LowN B 24/06/2013 0.416450145"
Here is the code to import and create the plot :
NDVI_ts <- read.table(text=dat, header = TRUE)
library(ggplot2)
library(scales)
interact<-interaction(NDVI_ts$Treatment, NDVI_ts$Date, sep=" : ")
ggplot(data=NDVI_ts, aes(x=interact, y=NDVI)) +
geom_boxplot(aes(fill = Trial.group), width = 0.6) +
theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
This code give me the following boxplot, which is fine, but the x-axis is not linked to dates : (NDVI ~ Treatment + Date + Trial.group)
I know that I can normally do that with something like this :
q + scale_x_date(breaks="1 week", labels=date_format("%d-%b"))
but the interact
matrix is a factor and cannot be defined as a time object, so that doesn't work. I have the following error :
Error: Invalid input: date_trans works with objects of class Date only
How could I have multivariate boxplot positions defined by dates?
The NDVI_ts$Date is already defined as a date-object in R.