在条形图中,每个条形代表单个分类变量的一个类别,但在堆积条形图中,条形代表相同的分类变量,但每个都分为子类别。如果我们想在每个条形图中有相似的颜色分布,那么可以使用带有 barplot 函数的 col 参数。
M1<−matrix(sample(c(1:5),20,replace=TRUE),ncol=4) M1输出结果
[,1] [,2] [,3] [,4] [1,] 4 3 5 3 [2,] 4 2 1 4 [3,] 5 5 2 3 [4,] 3 2 2 4 [5,] 1 5 2 5
barplot(M1,col=rep(c("white","green","red","blue","yellow"),5))输出结果
M2<−matrix(sample(c(26:30),20,replace=TRUE),ncol=4) M2
[,1] [,2] [,3] [,4] [1,] 28 27 27 29 [2,] 30 28 28 27 [3,] 28 28 30 29 [4,] 30 28 28 28 [5,] 26 28 29 30
barplot(M2,col=rep(c("white","green","red","blue","yellow"),5))输出结果
M3<−matrix(rpois(20,5),ncol=4) M3输出结果
[,1] [,2] [,3] [,4] [1,] 6 8 4 5 [2,] 8 3 7 2 [3,] 5 4 4 2 [4,] 5 3 5 2 [5,] 6 5 2 3
barplot(M3,col=rep(c("white","green","red","blue","yellow"),5))输出结果