0

How can I determine the y-value of the upper plot frame border in base R?

For example, in the plot below the y-value of the upper frame border is not 11.1, the maximum value of ylim. From trial and error the y-value of the upper frame border appears to be a tiny bit larger than 11.53162 which can be seen if the following line is added to the R code below:

 lines(T,  rep(11.53162, length(x)), type = 'l', col = 'brown', lwd = 2, lty = 3)

How can I obtain that value from R without using trial-and-error?

T   <- 1:10
x   <- 5 + 0.5 * T
x.u <- 6 + 0.5 * T
x.l <- 4 + 0.5 * T

jpeg('my_plot.jpg')

c(0, (max(x.u)+0.1))
# 0.0 11.1

plot(T, x, xlab = 'x', ylab = 'y', type = 'l', col = 'green', lwd = 2, lty = 1,

     ylim = c(0, (max(x.u)+0.1)), cex.lab = 1.5)
     lines(T,  x.u, type = 'l', col = 'brown', lwd = 2, lty = 3)
     lines(T,  x.l, type = 'l', col = 'brown', lwd = 2, lty = 3)
     lines(T,  rep(11.1, length(x)), type = 'l', col = 'brown', lwd = 2, lty = 3)

dev.off()

enter image description here

Mark Miller
  • 12,483
  • 23
  • 78
  • 132
  • 1
    Use `par("usr")[4]`. `plot(x = 1, y = 1, ylim = c(0, 2))`; `lims <- par("usr")`; `points(x = 1, y = lims[4], col = "red", cex = 2)` – Henrik Nov 23 '17 at 08:04
  • If I type that line below the plot code it returns the value 11.544 which seems too large for my example. – Mark Miller Nov 23 '17 at 08:10
  • 2
    "Seems"? I tried `plot(T, x, xlab = 'x', ylab = 'y', type = 'l', col = 'green', lwd = 2, lty = 1, ylim = c(0, (max(x.u)+0.1)), cex.lab = 1.5)`; `lims <- par("usr")`; `points(x = 6, y = lims[4], col = "red", cex = 2, pch = 4)`. I you don't think the point have the desired `y`, you may wish to clarify what you mean by "border". Cheers. – Henrik Nov 23 '17 at 08:16
  • @Henrik Thanks. Your second example is pretty convincing. – Mark Miller Nov 23 '17 at 08:31
  • When creating two plots I was not able to get the `usr` option to work. I was able to get Greg Snow's solution to work: `par(mfrow=c(2,1), xpd=NA);plot(x = 1, y = 1, ylim = c(0, 2));top.y <- grconvertY(1, from='npc');points(x = 1, y = top.y, col = "red", cex = 2, pch=4);plot(x = 10, y = 10, ylim = c(0, 20));top.y <- grconvertY(1, from='npc');points(x = 10, y = top.y, col = "red", cex = 2, pch=4);` – Mark Miller Dec 03 '17 at 07:19

0 Answers0