#数据
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
#调整间距,边距分别是:上、右、下、左
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个单位的长度。