I would like to plot multiple boxplots in R in a single graph and group them by pairs. I am a beginner in R and although several threads deal with the same subject (multiple boxplots in R), I could not find a comprehensive way to to this. I would like to use base graphics only if possible.
I have 10 sets of values, all of them containing 30 values (which can be any positive value). Within those 10 sets, 5 are of type 1 and the 5 other are of type 2. My objective is to have ten boxplots (one per set of values) and group each set of type 1 with a set of type 2 in a single graph. In the end, I would like to have 5 clusters of data, each of them containing two boxplots. I would also like to have 1 x-label per cluster (A,B,C,D,E) and to have the data of type 1 in red and the data of type 2 in green.
So far my code is:
A1data <- read.table("A1data.csv",header=TRUE,sep=";")
B1data <- read.table("B1data.csv",header=TRUE,sep=";")
C1data <- read.table("C1data.csv",header=TRUE,sep=";")
D1data <- read.table("D1data.csv",header=TRUE,sep=";")
E1data <- read.table("E1data.csv",header=TRUE,sep=";")
A2data <- read.table("A2data.csv",header=TRUE,sep=";")
B2data <- read.table("B2data.csv",header=TRUE,sep=";")
C2data <- read.table("C2data.csv",header=TRUE,sep=";")
D2data <- read.table("D2data.csv",header=TRUE,sep=";")
E2data <- read.table("E2data.csv",header=TRUE,sep=";")
A1 <- 100*(A1data$x-A1data$y)/A1data$x
B1 <- 100*(B1data$x-B1data$y)/B1data$x
C1 <- 100*(C1data$x-C1data$y)/C1data$x
D1 <- 100*(D1data$x-D1data$y)/D1data$x
E1 <- 100*(E1data$x-E1data$y)/E1data$x
A2 <- 100*(A2data$x-A2data$y)/A1data$x
B2 <- 100*(B2data$x-B2data$y)/B1data$x
C2 <- 100*(C2data$x-C2data$y)/C1data$x
D2 <- 100*(D2data$x-D2data$y)/D1data$x
E2 <- 100*(E2data$x-E2data$y)/E1data$x
A <- cbind(A1,A2)
B <- cbind(B1,B2)
C <- cbind(C1,C2)
D <- cbind(D1,D2)
E <- cbind(E1,E2)
test <- cbind(A,B,C,D,E)
boxplot(test,col=c(2,3),legend(1000,10,c("type 1","type 2)))
Which produces the error "Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet". However, if I change the last line with:
boxplot(test,col=c(2,3))
I obtain 10 boxplots correctly colored but I cannot find a way to group them by pairs, nor to put the correct legend. Is there a simple way to do this or do I need to organize my data differently? If so, thank you for pointing out the right direction.