关注“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个单位的长度。
![](https://i-blog.csdnimg.cn/blog_migrate/dd0180a6a8fdf149b91710897e3a6db8.png)