0

I want to create boxplots in R for each row of A and B (two matrices). I want to have them both in the same plot because they share same x-axis.

Here is my Data (each matrix has 20 rows, 5 columns)

A <- matrix( rnorm(100), ncol = 5 )
B <- matrix( rnorm(100), ncol = 5 )

For each row I want to have a boxplot. To create the boxplots for each matrix (row-wise, based on How to boxplot row-wise matrix in R? ) I can use:

boxplot(A, use.cols = F, col="red")
boxplot(B, use.cols = F, col="green")

I have tried this but the boxplots are not side by side (overlapping):

boxplot(A, use.cols = F, col="red")
par(new=TRUE)
boxplot(B, use.cols = F, col="green")

Any suggestions? Thanks.

2 Answers2

0

I guess this works:

boxplot(A, use.cols = F, col = "red", par(mfrow = c(1,2)))
boxplot(B, use.cols = F, col = "green")
krpa
  • 84
  • 1
  • 13
  • Hi. unfortunately is not what I want. I would like to have one plot. Since each matrix has 20 rows, I will end up with 20 boxplots for matrix A and 20 boxplots for matrix B. So I would like to have for x-axis 1,2,3, ...20 and then for example when x=1 I will have two box-plots red and green (represent row 1 of matrix A and row one of matrix B). – D.Derek May 30 '18 at 18:59
0

Here's a simple example that can produce your desired plot. This includes two horizontal boxplots, one on top of the other, with the same x-axis:

data(iris)
A = data.frame(X="A",
               Y=iris$Sepal.Length,
               stringsAsFactors=F)
B = data.frame(X="B",
               Y=iris$Sepal.Width,
               stringsAsFactors=F)
df = rbind(A,B)
par(mfrow=c(1,1))
boxplot(Y~X,
        data=df,
        horizontal=T,
        col=c("red","green"),
        las=2,
        xaxt="n")
axis(1,at=seq(0,10,1),labels=seq(0,10,1))

enter image description here

Dale Kube
  • 1,400
  • 13
  • 24
  • Hello. Thanks for your comment. I actually want to end up with one plot where the x-axis will be the 1,2,..19, 20 and the y-axis will show the values that corresponds to the box-plots. In the plot I will have actually 20 red and 20 green boxplots. Each box-plot will represent each row of the matrix – D.Derek May 30 '18 at 18:51
  • This solution does that. Are you mixing up your x and y axes? The x-axis goes from left to right, and the y-axis goes bottom to top. – Dale Kube May 30 '18 at 18:53
  • I would like to end up with a plot like the code that I wrote in the first message but unfortunately the red box-plots overlaps green and vice-versa. – D.Derek May 30 '18 at 18:56