Logo Questions Linux Laravel Mysql Ubuntu Git Menu

R Graphics: axis label placement relative to tick labels?




I wanted to make a simple R plot with y-axis labels centered above the y-axis tick labels. I created something I liked with the code below. But it required some fumbling with the at graphical parameter.

Q: Is there a less hacky way of doing this? Is there a way to query the y-axis tick labels for their width so that I could use that information to center the y-axis label above them?

n.obs       <- 390
vol.min     <- .20/sqrt(252 * 390)
eps         <- rnorm(n = n.obs, sd = vol.min)
windows(width = 5.05, height = 3.8)
plot(eps,  main  =  "Hello  World!", las=1, ylab="")
mtext(text="eps", side=3, at=-60)

my plot

like image 657
lowndrul Avatar asked Nov 05 '22 11:11


1 Answers

You can get the extent of the user coordinates with `par("usr") and then convert the units of margins. You have added an extra wringle by making the plot area non-standard. On a standard 7 x 7 device this works:

mtext(text="eps", side=3, at=usrcoord.x.left-0.075*diff(range(par("usr")[1:2]) ))

But on your smaller plot window, you need to use:

mtext(text="eps", side=3, at=usrcoord.x.left-0.1*diff(range(par("usr")[1:2]) ))
like image 50
IRTFM Avatar answered Nov 09 '22 15:11