R语言学习——柱形图

a <- read.table('/Users/zhangzhishuai/Downloads/25 R散点图和折线图(一)/25-28节/BMI.txt', sep = '\t',header = T, row.names = 1)
a

# 绘制柱形图
barplot(a$height)
barplot(a$height,
        names.arg = rownames(a), # 添加每个柱子下面的名字
        ylim = c(0,200), # 修改纵轴
        las=3, # 1:横着放柱子名字,2:竖着放柱子名字,3:横纵坐标都竖直
        xlab = 'name',
        ylab = 'height',
        main = 'height'
        )
par(mar=c(5,4,4,7)) # 控制页边距,下,左,上,右
barplot(a$height,main = 'height', ylim = c(0,200))
par(mfrow=c(3,2)) # 一个页面画(行,列)的图
p = barplot(a$height,
            names.arg = rownames(a),
            ylim = c(0,200),
            las=2
            )
p # 每个柱子正中间x坐标的位置
text(p,a$height+10,a$height,
     srt=90, # 旋转方向
     col="red") # text(横坐标,纵坐标,内容)

# 自定义x轴标签的大小,位置和角度
a <- read.table('/Users/zhangzhishuai/Downloads/25 R散点图和折线图(一)/25-28节/BMI.txt', sep = '\t',header = T, row.names = 1)
barplot(a$height,ylim = c(0,200))
p <- barplot(a$height,
        names.arg = rownames(a),
        ylim = c(0,200),
        axisnames = F # 不写轴名字
)
text(p,-5,labels = rownames(a),
     srt=30, # 控制角度
     cex=1, # 控制字体大小
     adj=c(1,1), # 往下1,往左1微调位置
     xpd = TRUE, # 因为已经画到x轴下面,也就是图外了,所以要加
     col='red'
     )

# 堆积柱形
p=barplot(
  as.matrix(t(a[,-3])),
  col = c('red','blue','green'),
  ylim = c(0,300)
)
legend('top', # 位置
       horiz = T, # 图注横着放
       bty = 'n', #去掉外面的框
       legend = names(a)[-3],
       col = c('red','blue','green'),
       pch = 15
       )

#并排柱形(分组柱状图)
p=barplot(
  as.matrix(t(a[,-3])),
  col = c('red','blue','green'),
  ylim = c(0,300),
  beside = T # 将柱子并排起来排
)
legend('top', # 位置
       horiz = T, # 图注横着放
       bty = 'n', #去掉外面的框
       legend = names(a)[-3],
       col = c('red','blue','green'),
       pch = 15
)

# 自定义x轴标签,大小位置和角度
data=t(as.matrix(a[,-3]))
p=barplot(
  data,
  beside = T,
  col = c('red','blue','green'),
  ylim = c(0,300),
  axisnames = F
)
text(p[2,],# 横坐标为第二行也就是中间柱子坐标
     0,
     labels = row.names(a),
     srt=45,
     cex = 1,
     adj = c(1,1),
     xpd=TRUE
     )

# error bar
male=a[a$gender=='male',-3]
female = a[a$gender=='female',-3]
male_mean = apply(male,
                  2,# 1:按照行来处理,2:按照列来处理
                  mean)
male_sd = apply(male,2,sd)
female_mean = apply(female,
                  2,# 1:按照行来处理,2:按照列来处理
                  mean)
female_sd = apply(female,2,sd)
data=rbind(male_mean,female_mean)
std = rbind(male_sd,female_sd)
p = barplot(data,
        beside = T,
        ylim = c(0,200),
        col=c('blue','red'))
# 添加误差棒
arrows(
  x0=p,y0=data+std,#给定误差棒第一个点横纵坐标
  x1=p,y1=data-std,#给定误差棒第二个点横纵坐标
  angle = 90, # 调整箭头和直线夹角
  code = 3, # 线类型
  length = 0.08,# 误差棒横线长度
  lwd = 0.5
)

legend('top', # 位置
       horiz = T, # 图注横着放
       bty = 'n', #去掉外面的框
       legend = c('height','weight'),
       col = c('red','green'),
       pch = 15
)



BMI.txt:
name height weight gender BMI
tom 180 75 male 23.1481481481481
cindy 165 58 female 21.3039485766759
jimmy 175 72 male 23.5102040816327
sam 173 68 male 22.7204383708109
lucy 160 60 female 23.4375
lily 165 55 female 20.2020202020202

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值