Get border border values โ€‹โ€‹()

I generate a lot of graphs with xlim and ylim , which I calculate for each plot. I want my legend to be outside the plot area (just above the field around the actual plot), but I cannot figure out how to get the maximum y-value of the field around my area.

Is there even a way for this? I can move the legend where I want it, manually changing the values โ€‹โ€‹of legend() x and y, but it takes a lot of time for the number of graphs that I create.

Thanks!

-JM

+6
source share
3 answers

Here is a basic example illustrating what I think you are looking for using one of the code examples from ?legend .

 #Construct some data and start the plot x <- 0:64/64 y <- sin(3*pi*x) plot(x, y, type="l", col="blue") points(x, y, pch=21, bg="white") #Grab the plotting region dimensions rng <- par("usr") #Call your legend with plot = FALSE to get its dimensions lg <- legend(rng[1],rng[2], "sin(cx)", pch=21, pt.bg="white", lty=1, col = "blue",plot = FALSE) #Once you have the dimensions in lg, use them to adjust # the legend position #Note the use of xpd = NA to allow plotting outside plotting region legend(rng[1],rng[4] + lg$rect$h, "sin(cx)", pch=21, pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA) 

enter image description here

+10
source

The par('usr') command will return the bounding box coordinates, but you can also use the grconvertX and grconvertY . A simple example:

 plot(1:10) par(xpd=NA) legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1) legend( grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0, xjust=1, legend='something', lty=1) 
+3
source

The ohm, omd, and omi arguments to par() control the boundaries and fields of the graphs - they can be requested using par()$omd (etc.). and set (if necessary) with par(oma=c()) (where the vector can have up to 4 values โ€‹โ€‹- see par)

+1
source

Source: https://habr.com/ru/post/896724/


All Articles