简体   繁体   中英

How to boxplot row-wise matrix in R?

I have a matrix with 8 columns. For each row I'd like to a plot a single boxplot. I prefer the boxplots to be in a single plot. So the following example should produce 4 boxplots (8 values each) - all in a single image.

Data example:

> data[2:5,]
     [,1] [,2] [,3]      [,4]      [,5]      [,6]      [,7]      [,8]     
[1,] 0.6  0.5  0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[2,] 0.5  0.5  0.5357143 2.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[3,] 0.5  0.7  0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[4,] 0.5  0.5  1.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185

So far I've tried:

> boxplot(data[2:5,])
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic

and this approach from this SO post :

> boxplot(as.list(as.data.frame(data[2:5,])))
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
   'x' must be atomic

I've been struggling for ages. Could you please give me hint?

EDIT1:

> dput(data[2:5,])
structure(list(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.535714285714286, 0.535714285714286, 
    0.535714285714286, 0.518518518518518, 0.518518518518518, 
    0.518518518518518, 0.518518518518518), .Dim = c(4L, 8L))

为了从矩阵中绘制出boxplot.matrix图,我们可以使用boxplot.matrix函数:

boxplot.matrix(data, use.cols = FALSE)

I think you need to use the t() function to transpose that matrix, since R generally does matrix operations on a column basis:

nums<-scan(text=" 0.6  0.5  0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
 0.5  0.5  0.5357143 2.5357143 0.5357143 0.5357143 0.5357143 0.5185185
 0.5  0.7  0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
 0.5  0.5  1.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185")
Read 32 items

 mat<- matrix(nums, nrow=4,byrow=TRUE)
 mat
     [,1] [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]
[1,]  0.6  0.5 0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[2,]  0.5  0.5 0.5357143 2.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[3,]  0.5  0.7 0.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
[4,]  0.5  0.5 1.5357143 0.5357143 0.5357143 0.5357143 0.5357143 0.5185185
> boxplot(mat)   # Not correct
> boxplot( t(mat) )

在此处输入图片说明

After the edit we now can see that the data -object is a rather strange one. It is a list with a dimension attribute so it gets printed as a matrix but it doesn't behave properly when passed to other functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM