[R] How do I move the axis labels precisely
Hurr
hill0093 at umn.edu
Thu May 22 18:44:39 CEST 2014
#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); print(horAxisLims)
par(mar=c(8,4,4,2)+.1,lheight=0.7) #margins=c(bot,lef,top,rit);
default:c(5,4,4,2)+.1
#vertical text space = char height * character expansion * lheight
#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=3.5;
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
prdTicLocs=c(1,365.,1252,1461,1753,2191,2922,4383,7000)
prdLabels=c("1Yr","1Da","7Hr","6Hr","5Hr","4Hr","3Hr","2Hr","1.25\nHr");
print(prdLabels)
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(round(frqTicLocs[i]),digits=4,trim=TRUE,scientific=FALSE)
print(frqLabels)
#default mgp=c(3,1,0) margin line in mex units
c(axisTitle,axisLabel,axisLine)
las=2; #las=2 makes axis labels perpendicular to axis
plot(horData,verData,xaxt='n',xlim=horAxisLims,xlab="")
#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=-.08)
staxlab(1,top.line=(prdAxDistDown),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=-.08)
staxlab(1,top.line=(frqAxDistDown),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/How-do-I-move-the-axis-labels-precisely-tp4691082.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list