R-barplot()

barplot这个函数啊。。。坑。。。度娘的很多解决方案都不对,只好重新看回manual再做测试==

本文参考的是:

https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/par.html
https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/barplot.html

# for colorspace
library(RColorBrewer)
# read data
data <- as.matrix(read.table("./species.txt",header=T,check.names=F))
colors = c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628","#F781BF", "#999999", "#1B9E77", "#D95F02","#7570B3", "#E7298A", "#66A61E", "#E6AB02", "#A6761D", "#666666", "#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854", "#FFD92F", "#E5C494", "#B3B3B3", "#1A1A1A", "#67001F")
labs=colnames(data)
# new a pdf
pdf("./species_taxonomy_barplot.pdf",width=40,height=20)
# 边空的大小由mai参数或mar参数控制,它们都是四个元素的向量,分别规定下方、左方、上方、右方的边空大小,其中mai取值的单位是英寸,而mar的取值单位是文本行高度。
par(mai=c(0.7,1,0.3,20))
# beside=F means stacked bars
# border=NA means omit borders of bars
# xasx=i means x-axis style: internal
# Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range.
# Style "i" (internal) just finds an axis with pretty labels that fits within the original data range.
# mgp: margin line
# las=2 means perpendicular to axis
# cex.names : expansion factor for axis names (bar labels).
# axes=F no axes drawn
# cex.lab=1.2 放大率
# xaxt : Specifying "n" suppresses plotting of the axis. The standard value is "s"
rownames(data)=data[,1]
bp = barplot(data, legend.text=F,beside=F, xlab="", col=colors, border=NA, xaxs="r", mgp=c(3,0.2,0.5), las=2, axes=F, ylab="Relative abundance (%)",cex.lab=1,xaxt="s")
# tcl=-0.2 means The length of tick marks as a fraction of the height of a line of text.
axis(2, las=2, cex.axis=1, mgp=c(3,0.4,0.5), tcl=-0.2)
# usr : A vector of the form c(x1, x2, y1, y2) giving the extremes of the user coordinates of the plotting region. 
#cex_x = ((par("usr")[2]-par("usr")[1])/29)*0.6
# xpd=TRUE means all plotting is clipped to the figure region.
# cex是放大倍数
# adj=1 means right-justified text
# srt: string rotation degree
#text(bp, par("usr")[3]-0.4,labels = colnames(data),xpd=TRUE,cex=1,adj=1,srt=45,font=0.1)
legend(par("usr")[2],par("usr")[4]+1,cex=0.9,bty="n",x.intersp=0.4,pt.cex=1,rownames(data),fill=colors,text.font=1,ncol=4,xpd=TRUE)
abline(h=axTicks(2),lty=2,col=rgb(0,0,0,0.2))
dev.off()

  

原文地址:https://www.cnblogs.com/pxy7896/p/6756444.html