I have something like the following:
x <- 1:5
y <- 2:6
A <- matrix(NA,nrow=100,ncol=5)
for(i in 1:5){A[,i] <- rnorm(100,x[i],y[i])}
B <- matrix(NA,nrow=100,ncol=5)
for(i in 1:5){B[,i] <- runif(100,min=x[i],max=y[i])}
The following command creates a boxplot for the 5 columns of matrix A:
boxplot(A[,1:5])
What I would like to do now is to have a boxplot like this, where each boxplot of a column of A is plotted next to a boxplot of the corresponding column of B. The boxplots should be directly next to each other, and between pairs of boxplots of the columns 1 to 5 there should be a small distance.
Thanks in advance!
library(reshape2);d2 <- cbind(melt(A), value2=melt(B)[,3]); library(ggplot2); dM <- melt(d2, id.var=c("Var1", "Var2")); dM$Var2 <- factor(dM$Var2); ggplot(dM, aes(x=Var2, y=value, fill=variable))+ geom_boxplot()+scale_fill_manual(values=c("blue", "red"))
– akrun