如何使用 barplot 函数在 R 中创建每个条形具有唯一颜色的堆积条形图?
在条形图中,每个条形代表单个分类变量的一个类别,而在堆积条形图中,条形代表相同的分类变量,但每个条形分为子类别。如果我们希望每个条形中颜色的分布相似,则可以使用 barplot 函数的 col 参数。
示例1
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
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
barplot(M1,col=rep(c("white","green","red","blue","yellow"),5))
输出
示例2
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))
输出
广告