barplot()作为1维图,输入值为单个向量或单个矩阵(分组条形图)
1. 单个向量
1.1
x=seq(10,by=2)
y=rep(c("a","b"),times=c(2,3))
barplot(height=x,names.arg = y,col=rainbow(5))
barplot是1维图,
- height 输入的向量
- names.arg 标签
- col 颜色,此处设置为彩虹色
1.2
barplot(height=x,names.arg = y,col=rainbow(5),
horiz = T,
xlab = "a",
ylab = "b",
border = F)
- horiz 此处翻转坐标轴
- xlab与ylab显示横纵轴的标签
- border 此处把bar的边框设置为无。
1.3 坐标标签的朝向
在前面的代码加入las
las=1
barplot(height=x,names.arg = y,col=rainbow(5),
horiz = T,
xlab = "a",
ylab = "b",
border = F,
las=1)
- las 坐标的标签的朝向,1:都向上、2:朝向图、3:
las=2
las=3
2. 输入值为单个矩阵
- 分组条形图
- 对矩阵按行分组
2.1堆砌
2.1.1 两行的矩阵
z=matrix(1:10,nrow=2)
y=rep(c("a","b"),times=c(2,3))
barplot(height = z,names.arg = y,col=rainbow(9),
border = F,
las=1)
2.1.2 三行的矩阵
z=matrix(1:15,nrow=3)
y=rep(c("a","b"),times=c(2,3))
barplot(height = z,names.arg = y,col=rainbow(9),
border = F,
las=1)
- 若想对矩阵按照列分组,可以将矩阵转置一下
t(matrix)
再输入到barplot
2.2 not堆砌
- beside=T
z=matrix(1:15,nrow=3)
y=rep(c("a","b"),times=c(2,3))
barplot(height = z,names.arg = y,col=rainbow(3),
border = F,
las=1,
beside = T)