R universal distance between plot limits and axis labels/title

Viewed 135

In the R plotting environment, is there a way to specify a universal distance between the placement of the axis label and axis name relative the the boundary of the plot? For example, consider the Y-axis of the following test plot:

pdf('test1.pdf',height=3.5,width=5.0)
plot(1,ylab=NA,yaxt='n',ann=F)
mtext('Y-Axis Label',side=2,line=1.5)
axis(2, pos=0.595,tick=F)
dev.off()

I have optimized the placement of the axis label and axis name using the line=1.5 and pos=0.595 parameters within the mtext and axis functions, respectively. However, once I generate a separate plot with a different width (i.e. width=8.0), the placement no longer resembles the physical distance that I specified in the original graph using a width of 5.0. So the questions is - how do I specify these parameters so that the label and name will be exactly the same distance from the graph boundary regardless of the plot width. Could this be specified using a universal unit (i.e. pixels)?

Thanks in advance!

1 Answers

try grconvertX but you have to keep track of how you need the output. mtext(..., line = ) is in lines whereas axis(..., pos = ) is expecting user coordinates. Also you would need to use grconvertY where appropriate (ie, on sides 1 and 3)

pdf('test1.pdf',height=3.5,width=5.0)
plot(1,ylab=NA,yaxt='n',ann=F)
mtext('Y-Axis Label',side=2,line=1.5)
axis(2, pos=0.595,tick=F)

line <- grconvertX(1.5, 'lines', 'device')
pos <- grconvertX(0.595, 'user', 'device')
dev.off()


pdf('test1-again.pdf',height=3.5,width=5)
plot(1,ylab=NA,yaxt='n',ann=F)
mtext('Y-Axis Label',side=2, line = grconvertX(line, 'device', 'lines'))
axis(2, pos = grconvertX(pos, 'device', 'user'), tick = FALSE)
dev.off()


pdf('test1-wide.pdf',height=3.5,width=8)
plot(1,ylab=NA,yaxt='n',ann=F)
mtext('Y-Axis Label',side=2, line = grconvertX(line, 'device', 'lines'))
axis(2, pos = grconvertX(pos, 'device', 'user'), tick = FALSE)
dev.off()
Related