[R] Second axis on bottom of graph

Hurr hill0093 at umn.edu
Wed May 21 13:05:55 CEST 2014


Next I want to move the x axis labels up towards the axis to 
make the gap smaller. The mgp= was suggested to me. 
The staxlab function to be standard soon was sent to me by 
the author to make staxlab work on the second x axis.
Runnable code to help demonstrate what I want to do:
Hurr

#trying for both period-labeled and frequency labeled horizontal axis 
rm(list=ls(all=TRUE))
install.packages('plotrix')
library(plotrix)
staxlab<-function(side=1,at,labels,nlines=2,top.line=0.5,
 line.spacing=0.8,srt=NA,ticklen=0.03,adj=1,...) {
 if(missing(labels)) labels<-at
 nlabels<-length(labels)
 if(missing(at)) at<-1:nlabels
 axislim<-par("usr")[3:4-2*side%%2]
 if(any(at < axislim[1]) || any(at > axislim[2]))
  warning("Some axis labels are off the plot")
 if(is.na(srt)) {
  linepos<-rep(top.line,nlines)
  for(i in 2:nlines) linepos[i]<-linepos[i-1]+line.spacing
  linepos<-rep(linepos,ceiling(nlabels/nlines))[1:nlabels]
  axis(side=side,at=at,labels=rep("",nlabels))
  mtext(text=labels,side=side,line=linepos,at=at,...)
 }
 else {
  linewidth<-strheight("M")
  xylim<-par("usr")
  if(side == 1) {
   xpos<-at
   if(par("ylog")) ypos<-10^(xylim[3]-ticklen*(xylim[4]-xylim[3]))
   else ypos<-xylim[3]-ticklen*(xylim[4]-xylim[3])-top.line*linewidth
  }
  if(side == 3) {
   xpos<-at
   if(par("ylog")) ypos<-10^(xylim[4]+ticklen*(xylim[4]-xylim[3]))
   else ypos<-xylim[4]+ticklen*(xylim[4]-xylim[3])+top.line*linewidth
  }
  if(side == 2) {
   ypos<-at
   if(par("xlog")) xpos<-10^(xylim[1]-ticklen*(xylim[2]-xylim[1]))
   else xpos<-xylim[1]-ticklen*(xylim[2]-xylim[1])-top.line*linewidth
  }
  if(side == 4) {
   ypos<-at
   if(par("xlog")) xpos<-10^(xylim[2]+ticklen*(xylim[2]-xylim[1]))
   else xpos<-xylim[2]+ticklen*(xylim[2]-xylim[1])+top.line*linewidth
  }
  par(xpd=TRUE)
  text(xpos,ypos,labels,srt=srt,adj=adj,...)
  par(xpd=FALSE)
 }
}
horAxisLims=c(0,7213); hAL1=horAxisLims[1]; print(hAL1);
print(horAxisLims[2]) 
verData=c(1,365,809,1252,1753,2191,2922,3409,3896,4383,4819,5255,5691,6128,6564,7000)
#not fussy 
horData=c(1,300,800,1200,1700,2100,2900,3400,3800,4300,4800,5200,5600,6100,6500,7000)
#not at tics 
horTicLocs=c(1,365.,1252,1461,1753,2191,2922,4383,7000)
horLabels=c("1Yr","1Da","7Hr","6Hr","5Hr","4Hr","3Hr","2Hr","1.25\nHr")
par(mar=c(8,4,4,2)+.1,lheight=0.7)  #c(bot,lef,top,rit);
default:c(5,4,4,2)+.1 
#default mgp=c(3,1,0) margin line in mex units
c(axisTitle,axisLabel,axisLine) 
#plot(0, mgp=c(3,2,0), las=2) 
plot(horData,verData,xaxt='n',xlim=horAxisLims,xlab="",mgp=c(3,5,0))
axis(1,tick=TRUE,at=horTicLocs,labels=rep("",length(horLabels)),padj=0,tck=-.1)
staxlab(1,at=horTicLocs,labels=horLabels,srt=90,adj=c(1,0)) 
nFrqTicInvls=9; frqTicLocs <- vector(length=nFrqTicInvls+1); frqLabels <-
vector(length=nFrqTicInvls+1); 
frqInvl=(horAxisLims[2]-horAxisLims[1])/nFrqTicInvls 
for(i in 1:(nFrqTicInvls+1))frqTicLocs[i]=horAxisLims[1]+(i-1)*frqInvl 
for(i in
1:(nFrqTicInvls+1))frqLabels[i]=format(frqTicLocs[i],digits=4,trim=TRUE,scientific=FALSE) 
for(i in 1:(nFrqTicInvls+1))print(frqLabels[i]) 
#tck if >= 0.5 then fraction of relevant side; if =1 then gridlines 
axis(1,tick=TRUE,line=3.5,at=frqTicLocs,labels=rep("",length(frqLabels)),padj=0,tck=-.1) 
staxlab(1,top.line=5,at=frqTicLocs,labels=frqLabels,srt=90,adj=c(1,0)) 
title(xlab="Cycles/Yr",line=6)




--
View this message in context: http://r.789695.n4.nabble.com/Second-axis-on-bottom-of-graph-tp4690696p4690962.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list