[R] Second axis on bottom of graph

Hurr hill0093 at umn.edu
Wed May 28 19:00:42 CEST 2014


Thank you Jeff for your good orientation. You are an educator.
I looked at the documentation for more than an hour.
Yes, there is a big learning curve. 
My R-coding friend doesn't know it either.
Here is runnable code for a graph I would like to redo in the better code.
The staxlab function was sent to me by the author.
The remainder code isn't so long.
An expert could rewrite it quickly. Any out there?

#trying for both period-labeled and frequency-labeled horizontal axes 
rm(list=ls(all=TRUE))
install.packages('plotrix'); library(plotrix) 
#install.packages('lattice'); library(lattice) 
#install.packages('ggplot2'); library(ggplot2) 
#staxlab (to be standard soon) sent by author so staxlab works on second x
axis.
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)
 }
}
par(lheight=0.7)#vertical text space = lheight * char height * character
expansion 
par(mar=c(8,4,4,2)+.1)  #margins: mar=c(bot,lef,top,rit);
default:c(5,4,4,2)+.1 
par(xaxs="i") #to make 2nd axis and plot width size match; others: xaxs=
"r", "i", "e", "s", "d". 
par(mgp=c(3,.5,0)) #default mgp=c(3,1,0) margin line in mex units
c(axisTitle,axisLabel,axisLine) 
#set only by
par():ask,fig,fin,lheight,mai,mar,mex,mfcol,mfrow,mfg,new,oma,omd,omi,pin,plt,ps,pty,usr,xlog,ylog,ylbias 
prdAxDistDown=0; frqAxDistDown=2.25; 

horAxFrqCntrlNums=c(-10.0,0.0,1.1,500.0,515.0)
#=c(lefGrafBordFrq,lefScalEndFrq,lefPlotEndFrq,ritPlotEndFrq,ritScalEndFrq)
horData=c(1.100,56.53,112.0,167.4,222.8,278.3,333.7,389.1,444.6,500.0)
verData=c(57.67,98.33,82.40,68.15,78.89,93.53,22.54,37.60,87.19,49.35)
prdTicLocs=c(1.10,12,52.2,91.3, 122, 183, 365,500)
prdLabels=c("10.9\nMo","1Mo","1Wk","4Da","3Da","2Da","1Da","17.5\nHr")

print(horAxFrqCntrlNums); print(prdLabels) 
nFrqTicInvls=9; frqTicLocs <- vector(length=nFrqTicInvls+1); frqLabels <-
vector(length=nFrqTicInvls+1); 
frqInvl=(horAxFrqCntrlNums[5]-horAxFrqCntrlNums[2])/nFrqTicInvls 
for(i in
1:(nFrqTicInvls+1))frqTicLocs[i]=horAxFrqCntrlNums[2]+(i-1)*frqInvl; 
for(i in
1:(nFrqTicInvls+1))frqLabels[i]=format(round(frqTicLocs[i]),digits=4,trim=TRUE,scientific=FALSE) 
print(frqTicLocs); print(frqLabels) 
las=2; #las=2 makes axis labels perpendicular to axis 
plot(horData,verData,xaxt='n',xlim=c(horAxFrqCntrlNums[1],horAxFrqCntrlNums[5]),xlab="",ylab="") 
axis(2,tick=TRUE,line=0,at=NULL) #side: 1=below, 2=left, 3=above and 4=right 
#tck if >= 0.5 then fraction of relevant side; if =1 then gridlines 
axis(1,tick=TRUE,line=prdAxDistDown,at=prdTicLocs,labels=rep("",length(prdLabels)),padj=0,tck=-.04) 
staxlab(1,top.line=(prdAxDistDown-0.8),at=prdTicLocs,labels=prdLabels,srt=90,adj=c(1,0)) 
axis(1,tick=TRUE,line=frqAxDistDown,at=frqTicLocs,labels=rep("",length(frqLabels)),padj=0,tck=-.04) 
staxlab(1,top.line=(frqAxDistDown+0.2),at=frqTicLocs,labels=frqLabels,srt=90,adj=c(1,0)) 
title(xlab="Cycles/Yr",line=4); title(ylab="verData",line=1.5); 
title(main="Trying Both Frequency And Period Axes") 




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



More information about the R-help mailing list