如何在 R 中绘制逐行矩阵的箱线图?
How to boxplot row-wise matrix in R?
我有一个有 8 列的 matrix
。对于每一行,我想绘制一个箱线图。我更喜欢将箱线图放在一个图中。因此,以下示例应生成 4 个箱线图(每个箱线图 8 个值)- 全部在一个图像中。
数据示例:
> 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
到目前为止我已经尝试过:
> boxplot(data[2:5,])
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
'x' must be atomic
这种方法来自 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
我已经奋斗了很多年。你能给我提示吗?
编辑 1:
> 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(data, use.cols = FALSE)
我认为您需要使用 t() 函数来转置该矩阵,因为 R 通常以列为基础进行矩阵运算:
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) )
编辑后我们现在可以看到data
对象是一个比较奇怪的对象。它是一个具有维度属性的列表,因此它被打印为矩阵,但在传递给其他函数时表现不正常。
我有一个有 8 列的 matrix
。对于每一行,我想绘制一个箱线图。我更喜欢将箱线图放在一个图中。因此,以下示例应生成 4 个箱线图(每个箱线图 8 个值)- 全部在一个图像中。
数据示例:
> 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
到目前为止我已经尝试过:
> boxplot(data[2:5,])
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
'x' must be atomic
这种方法来自 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
我已经奋斗了很多年。你能给我提示吗?
编辑 1:
> 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(data, use.cols = FALSE)
我认为您需要使用 t() 函数来转置该矩阵,因为 R 通常以列为基础进行矩阵运算:
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) )
编辑后我们现在可以看到data
对象是一个比较奇怪的对象。它是一个具有维度属性的列表,因此它被打印为矩阵,但在传递给其他函数时表现不正常。