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?
set.seed(1)
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)
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]) ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With