R多行交叉作图

时间:2023-12-22 10:33:44

#中文 UTF-8编码   3.4.0
library(Hmisc)
mydata = read.table('clipboard',header = T)
head(mydata)
mycol=c(rgb(1,0,0,0.3),'cadetblue','darkseagreen',
        'aquamarine3','darkslateblue','red','blue')
coltextandlabel = 'black'
####################################################################
dev.new()
close.screen(all.screens = T)
split.screen(c(1,1))
mybottom = c(seq(0.7,0.25,length.out = 4),0.12,0)
mytop = c(seq(1,0.45,length.out = 4),0.25,0.13)
mylayout = matrix(data = c(rep(0,6),rep(1,6),mybottom,
                           mytop),ncol = 4)
split.screen(mylayout)
mtck =-0.03;mmgp = c(3.5,0.5,0);mline = 2.5; mlwd = 2
#########################################################
id=2
screen(id)#准备绘制第2号屏幕,也是最上面的图
par(tck = mtck,mar=c(0,4,2,4),mgp=mmgp,las=1)
yl = range(na.exclude(mydata$NO3))
plot(x=mydata$depth,y=mydata$NO3,type='l',axes=F,lwd=mlwd,
     ylab='',xlab='',col=mycol[id],ylim=yl)
#axis(4,at=seq(0,4.8,length.out = 4),col=mycol[id],col.axis=mycol[id])
axis(4,at=seq(0,4.8,length.out = 4))
mtext(side = 4,line = mline,las=0,
      text =  expression(paste(NO[3]^'-',' (μeq/L)')))
#########################################################
id=3
screen(id)#准备绘制第3号屏幕,也是最上面的图
par(tck = mtck,mar=c(0,4,0,4),mgp=mmgp,las=1)
yl = range(na.exclude(mydata$NH4))
plot(x=mydata$depth,y=mydata$NH4,type='l',axes=F,lwd=mlwd,
     ylab='',xlab='',col=mycol[id],ylim=yl)
axis(2,at=seq(0,12,length.out = 4))
mtext(side = 2,line = mline,las=0,
      text = expression(paste(NH[4]^'+',' (μeq/L)')))
#########################################################
id=4
screen(id)#准备绘制第3号屏幕,也是最上面的图
par(tck = mtck,mar=c(0,4,0,4),mgp=mmgp,las=1)
yl = range(na.exclude(mydata$Ca))
plot(x=mydata$depth,y=mydata$Ca,type='l',axes=F,lwd=mlwd,
     ylab='',xlab='',col=mycol[id],ylim=yl)
axis(4,at=seq(0,5400,length.out = 4))
mtext(side = 4,line = mline,las=0,
      text = expression(paste(Ca^'2+',' (μeq/L)')))
#text = expression(paste(Ca^'2+','\n(μeq/L)'
#erase.screen(4)
#########################################################
id=5
screen(id)#准备绘制第3号屏幕,也是最上面的图
par(tck = mtck,mar=c(0,4,0,4),mgp=mmgp,las=1)
plot(x=mydata$depth,y=mydata$Mg,type='l',axes=F,lwd=mlwd,
     ylab='',xlab='',col=mycol[id])
axis(2,at=seq(0,600,length.out = 4))
mtext(side = 2,line = mline,las=0,
      text = expression(paste(Mg^'2+',' (μeq/L)')))
#########################################################
id=6
screen(id)#准备绘制倒数第二个屏幕,也是最上面的图
prdata = cbind(mydata$depth,mydata$precipitation.mm.)
prdatan = prdata[!is.na(prdata[,2]),]
range(prdatan[,2])
par(tck = mtck,mar=c(0,4,0,4),mgp=mmgp,las=1)
plot(x=prdatan[,1],y=prdatan[,2],type='l',axes=F,
     ylab='',xlab='',col=mycol[id])
axis(4,at=seq(250,500,length.out = 3),tck=-0.05)
mtext(side = 4,line = mline,las=0,
      text = 'Pr.(mm)')
#########################################################
id=7
screen(id)#准备绘制倒数第一个屏幕,也是最上面的图
wqldata = cbind(mydata$depth,mydata$w.q.l.mm.)
wqldatan = wqldata[!is.na(prdata[,2]),]
range(wqldatan[,2])
par(tck = mtck,mar=c(2.5,4,0,4),mgp=mmgp,las=1)
plot(x=wqldatan[,1],y=wqldatan[,2],type='l',axes=F,
     ylab='',xlab='',col=mycol[id])
axis(2,at=seq(400,800,length.out = 2),tck=-0.07)
mtext(side = 2,line = mline,las=0,
      text = 'w.q.l.(mm)')
#########################################################
id=1
screen(id,new=F)
par(tck = -0.01,mar=c(2.3,4,2,4),mgp=c(2.3,1,0))
plot(x=mydata$depth,y=mydata$Mg,type='n',axes=F,
     ylab='',xlab='',col=mycol[id])
axis(1,at=seq(0,2200,200),padj = -1)
mtext(side = 1,text = 'Depth (cm)',line = 1.3)
box()

majorvline=mydata$depth[which(mydata$THICK!='NA')]
abline(v= majorvline,col='orangered',lty=1,lwd=1.5)
minorvline=mydata$depth[which(mydata$THIN!='NA')]
abline(v= minorvline,col=mycol[id],lty=2,lwd=1)
at3=mydata$depth[which(mydata$alllabel!='NA')]
lab3=mydata$alllabel[which(mydata$THICK!='NA')]
mtext(side = 3,at=majorvline,text = c(lab3),cex=1,col='orangered')