美文网首页
barplot()条形图

barplot()条形图

作者: 东方不赞 | 来源:发表于2020-04-13 20:52 被阅读0次

barplot()作为1维图,输入值为单个向量或单个矩阵(分组条形图)

1. 单个向量

1.1

图片.png
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

图片.png
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

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=2

las=3

las=3

2. 输入值为单个矩阵

  • 分组条形图
  • 对矩阵按行分组

2.1堆砌

2.1.1 两行的矩阵

分组条形图-2行的矩阵
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 三行的矩阵

3行的矩阵
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)
图片.png

相关文章

网友评论

      本文标题:barplot()条形图

      本文链接:https://www.haomeiwen.com/subject/olqomhtx.html