旋转x标签,R中的轴边距没有空格

时间:2014-03-03 17:54:08

标签: r plot rgraph

我必须使用R中的mtext()将x轴标签旋转45° 我不能使用text(...)因为我设置了par(mar = c(0,0,0,5))。

par(mfrow=c(3,2),mar=c(0,5,0.5,0),mgp=c(2.5,0.2,0))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=1,lab=F,tck=0.01)

par(mar=c(0,0,0.5,5))
plot(c(202,270),c(0.1,0.4),type="n",xaxt="n",xlab="",ylab="",yaxt="n",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
axis(4,font=2,las=1,cex=0.8,cex.lab=0.8,cex.axis=.9,tck=.01)

par(mar=c(0,5,0,0))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)

par(mar=c(0,0,0,5))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="Date",ylab="",yaxt="n",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
axis(4,font=2,las=1,cex=0.8,cex.lab=0.8,cex.axis=.9,tck=.01)
x_names<-c("Jul 24","Jul 31","Aug 13","Aug 21","Sep 08","Sep 23")   
mtext(x_names, side=1, line=1, at=c(1,2,3,4,5,6),las=2)

par(mar=c(2.3,5,0,0))
plot(c(202,270),c(0.5,1.3),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
text(  c(205,212,225,233,251,267),  par("usr")[3]-.01, srt=45, adj=1, x_names, xpd=T)  

enter image description here

1 个答案:

答案 0 :(得分:2)

通过设置xpd = NA,即使轴边距没有空格,也可以使用text() 我已修改您的示例以在第4个图上显示x轴标签:

par(mfrow=c(3,2),mar=c(0,5,0.5,0),mgp=c(2.5,0.2,0))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=1,lab=F,tck=0.01)

par(mar=c(0,0,0.5,5))
plot(c(202,270),c(0.1,0.4),type="n",xaxt="n",xlab="",ylab="",yaxt="n",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
axis(4,font=2,las=1,cex=0.8,cex.lab=0.8,cex.axis=.9,tck=.01)

par(mar=c(0,5,0,0))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)

par(mar=c(0,0,0,5))
plot(c(1:6),c(1:6),type="n",xaxt="n",xlab="Date",ylab="",yaxt="n",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
axis(4,font=2,las=1,cex=0.8,cex.lab=0.8,cex.axis=.9,tck=.01)
x_names<-c("Jul 24","Jul 31","Aug 13","Aug 21","Sep 08","Sep 23")   
# Note that mtext() is commented and text() is used instead
#mtext(x_names, side=1, line=1, at=c(1,2,3,4,5,6),las=2)
text(1:6,  par("usr")[3]-.01, srt=45, adj=1, x_names, xpd=NA) 

par(mar=c(2.3,5,0,0))
plot(c(202,270),c(0.5,1.3),type="n",xaxt="n",xlab="",ylab="",tck=0.01)
axis(1,c(205,212,225,233,251,267),cex.axis=.9,lab=F,tck=0.01)
text(  c(205,212,225,233,251,267),  par("usr")[3]-.01, srt=45, adj=1, x_names, xpd=T)

结果:

enter image description here