1

假设您有 2 个矩阵m1m2并且每个矩阵都有相等的列数。

m1 = matrix(0, 10, 5, dimnames = list(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), c(1, 2, 3, 4, 5)))
m1[1,] = c(0,0,0,0,1)
m1[2,] = c(0,0,0,1,1)
m1[3,] = c(0,0,1,1,1)
m1[4,] = c(0,0,1,1,0)
m1[5,] = c(1,0,0,0,0)
m1[6,] = c(1,1,1,0,0)
m1[7,] = c(0,1,1,0,0)
m1[8,] = c(0,1,1,0,0)
m1[9,] = c(0,1,1,1,0)
m1[10,] = c(1,1,1,0,1)

m2 = matrix(0, 10, 5, dimnames = list(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), c(1, 2, 3, 4, 5)))
m2[1,] = c(0,0,0,0,1)
m2[2,] = c(0,0,0,1,1)
m2[3,] = c(0,0,1,1,1)
m2[4,] = c(0,0,1,1,0)
m2[5,] = c(1,0,0,0,0)
m2[6,] = c(1,1,1,0,0)
m2[7,] = c(0,1,1,0,0)
m2[8,] = c(0,1,1,0,0)
m2[9,] = c(0,1,1,1,0)
m2[10,] = c(1,1,1,0,1)

我想看到的是这两个矩阵的饼图比较。

我能想到的一种方法是为每一列添加每一行,然后使用这些来获取饼图的分数。

sumcols <-function(x){
    for (i in 1:numcols(x)){
        sum <- sum(x[,i])
        sums.append(sum) #python here ...
    }
    return(sums)
}

所以现在,我可以传递任何矩阵,取回总和列表,我假设现在我们可以使用它来获取饼图:

sums1 <- sumcols(m1)
sums2 <- sumcols(m2)
par(mfrow = c(1,2))
pie(c(sums1,sums2))

感谢您的帮助!

4

1 回答 1

3

这将为您提供第一列总和的饼图:

pie(c(colSums(m1)[1],colSums(m2)[1]))

我认为 barplot 会提供更多信息:

barplot(c(colSums(m1),colSums(m2)), col=c(rep(1,ncol(m1)),rep(2,ncol(m1))))

更新:

试试这个:

#get col sums
m1_sums <- colSums(m1)
m2_sums <- colSums(m2)
#make negatives zero
m1_sums[m1_sums<0] <- 0
m2_sums[m2_sums<0] <- 0
#pie
par(mfrow = c(1,2))
pie(m1_sums,main="m1 - colSums")
pie(m2_sums,main="m2 - colSums")

在此处输入图像描述

于 2013-11-12T10:45:31.613 回答