柱状图分区abline 顶部加上数量标签 图列修改 barplot函数 R语言

关注“R医学”,输入关键词,免费获取R语言画图资料

晓晓
2023-07-17
#数据
data <- data.frame(
  name = c("DD","with himself","with DC","with Silur" ,"DC","with himself","with DD","with Silur" ,"Silur","with himself","with DD","with DC" ),
  average = sample(seq(1,10) , 12 , replace=T),
  number = sample(seq(4,39) , 12 , replace=T)
)

data
##            name average number
## 1            DD       6     19
## 2  with himself       8     28
## 3       with DC       6     30
## 4    with Silur       2      4
## 5            DC       8      7
## 6  with himself       8      6
## 7       with DD       5      7
## 8    with Silur       3     10
## 9         Silur       5     25
## 10 with himself       2     24
## 11      with DD       3     20
## 12      with DC       4     23
#调整间距,边距分别是:上、右、下、左
par(mar=c(6,4,4,4))


#画图
my_bar <- barplot(data$average , 
                  border=F , 
                  names.arg=data$name , 
                  las=2,  # 1是水平,2是垂直
                  col=c(rgb(0.3,0.1,0.4,0.6) , rgb(0.3,0.5,0.4,0.6) , rgb(0.3,0.9,0.4,0.6) ,  rgb(0.3,0.9,0.4,0.6)) , 
                  ylim=c(0,13) , 
                  main="" )



#加线
abline(v=c(4.9 , 9.7) , col="grey")

#加文本
text(my_bar, 
     data$average+0.4 , 
     paste("n: ", data$number, sep="") ,
     cex=1) 

#图列
legend("topleft", 
       legend = c("Alone","with Himself","With other genotype" ) , 
       col = c(rgb(0.3,0.1,0.4,0.6) , rgb(0.3,0.5,0.4,0.6) , rgb(0.3,0.9,0.4,0.6) ,  rgb(0.3,0.9,0.4,0.6)) , 
       bty = "n",   # 边框类型,"n"代表无边框
       pch=20 ,     # 图例点的样式,这里使用实心圆点
       pt.cex = 2,  # 图例点的大小比例因子,这里设置为2倍大小
       cex = 0.8,   # 图例文字的大小比例因子,这里设置为0.8倍大小
       horiz = FALSE, # 图例的排列方式,这里是垂直排列
       inset = c(0.05, 0.05)) # 图例的位置偏移量,这里表示相对于绘图区域的左上角偏移量为0.05个单位的长度。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值