如何排序矩阵的barplot ?

时间:2023-01-18 12:51:54
data <- structure(list(W= c(1L, 3L, 6L, 4L, 9L), X = c(2L, 5L, 
4L, 5L, 12L), Y = c(4L, 4L, 6L, 6L, 16L), Z = c(3L, 5L, 
6L, 7L, 6L)), .Names = c("W", "X", "Y", "Z"),
     class = "data.frame", row.names = c(NA, -5L))
colours <- c("red", "orange", "blue", "yellow", "green")

barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", 
          cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours).

如何排序矩阵的barplot ?

That works fine but I need to order each group (separately) by decreasing, i.e. show the same plot but order from high to low for W, …, Z. Example: for W, green will be first from left, blue, yellow, … . For x, green will be first from left, orange, yellow and so on.

这个方法很好,但是我需要通过减少来对每一组(分别)进行排序,也就是说,对于W,…,z,从高到低,显示相同的图形。对于x,绿色是从左开始,橙色,黄色等等。

1 个解决方案

#1


3  

This can be achieved by generating the colors vector which has as many elements as there are bars and sorting each matrix column separately:

这可以通过生成颜色向量来实现,颜色向量的元素与条形的元素一样多,并分别对每个矩阵列进行排序:

sort the colors according to x order for each column and convert to vector:

根据每一列的x顺序对颜色进行排序,并转换为矢量:

colours <- as.vector(apply(data, 2, function(x){
  col <- colours[order(x)]
  }))

sort each column separetely:

区分每一列:

df <- apply(data, 2, sort)

barplot(df,
        main = "My Barchart",
        ylab = "Numbers",
        cex.lab = 1.5,
        cex.main = 1.4,
        beside = TRUE,
        col = colours)

如何排序矩阵的barplot ?

for descending order and with legend

用于降序和图例

colours <- c("red", "orange", "blue", "yellow", "green")

colours1 <- as.vector(apply(data, 2, function(x){
  col <- colours[order(x, decreasing = TRUE)]
  }))

barplot(apply(data, 2, sort,  decreasing = TRUE),
        main = "My Barchart",
        ylab = "Numbers",
        cex.lab = 1.5,
        cex.main = 1.4,
        beside = TRUE,
        col = colours1)

legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)

Here one color vector was used to color the bars and another for the legend

在这里,一个颜色向量被用来给条形图上色,另一个用于图例

如何排序矩阵的barplot ?

and finally for the question in the comments about aggregated data:

最后,关于聚合数据评论中的问题:

all <- apply(data, 1, mean)
colours <- c("red", "orange", "blue", "yellow", "green")

barplot(sort(all, decreasing = T), col = colours[order(all, decreasing = T)])

如何排序矩阵的barplot ?

#1


3  

This can be achieved by generating the colors vector which has as many elements as there are bars and sorting each matrix column separately:

这可以通过生成颜色向量来实现,颜色向量的元素与条形的元素一样多,并分别对每个矩阵列进行排序:

sort the colors according to x order for each column and convert to vector:

根据每一列的x顺序对颜色进行排序,并转换为矢量:

colours <- as.vector(apply(data, 2, function(x){
  col <- colours[order(x)]
  }))

sort each column separetely:

区分每一列:

df <- apply(data, 2, sort)

barplot(df,
        main = "My Barchart",
        ylab = "Numbers",
        cex.lab = 1.5,
        cex.main = 1.4,
        beside = TRUE,
        col = colours)

如何排序矩阵的barplot ?

for descending order and with legend

用于降序和图例

colours <- c("red", "orange", "blue", "yellow", "green")

colours1 <- as.vector(apply(data, 2, function(x){
  col <- colours[order(x, decreasing = TRUE)]
  }))

barplot(apply(data, 2, sort,  decreasing = TRUE),
        main = "My Barchart",
        ylab = "Numbers",
        cex.lab = 1.5,
        cex.main = 1.4,
        beside = TRUE,
        col = colours1)

legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)

Here one color vector was used to color the bars and another for the legend

在这里,一个颜色向量被用来给条形图上色,另一个用于图例

如何排序矩阵的barplot ?

and finally for the question in the comments about aggregated data:

最后,关于聚合数据评论中的问题:

all <- apply(data, 1, mean)
colours <- c("red", "orange", "blue", "yellow", "green")

barplot(sort(all, decreasing = T), col = colours[order(all, decreasing = T)])

如何排序矩阵的barplot ?