0

I have to rotate x axis labels by 45 using mtext() in R. I cannot use text(...) because I set 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

Mitra Rahmati
  • 291
  • 4
  • 10
  • 2
    I don't understand, if you set no line on the bottom margin and you use mtext you'll see nothing (as in your example). So why do you say "I cannot use text()" ? I suspect your real purpose is not to hide the left and bottom margins... please show an example of what you want to accomplish... – digEmAll Mar 03 '14 at 18:23
  • digEmAll please see my example. Indeed I have 5 plots on a single page while x axis label creates only for the last ones. – Mitra Rahmati Mar 04 '14 at 09:03
  • Ok, but what's you desired result ? Having the x labels on the last 2 plots on the bottom, just on the last... or something else ? – digEmAll Mar 04 '14 at 17:56
  • Yes I would like to rotate x labels on the last 2 plots by 45. Do you have any idea please? – Mitra Rahmati Mar 04 '14 at 19:35

1 Answers1

2

You can use text() even with no space on the axis margin, by setting xpd=NA.
I have modified your example to show the x axis labels on the 4th plot:

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)

Result :

enter image description here

digEmAll
  • 56,430
  • 9
  • 115
  • 140