Get plot() bounding box values

前端 未结 3 1982
盖世英雄少女心
盖世英雄少女心 2020-12-19 05:15

I\'m generating numerous plots with xlim and ylim values that I\'m calculating on a per-plot basis. I want to put my legend outside the plot area (

相关标签:
3条回答
  • 2020-12-19 05:41

    The oma, omd, and omi arguments of par() control boundaries and margins of plots - they can be queried using par()$omd (etc). and set (if needed) using par(oma=c()) (where the vector can have up to 4 values - see ?par)

    0 讨论(0)
  • 2020-12-19 05:43

    Here's a basic example illustrating what I think you're 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(c x)", 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(c x)", pch=21, 
                pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA)
    

    enter image description here

    0 讨论(0)
  • 2020-12-19 05:58

    The command par('usr') will return the coordinates of the bounding box, but you can also use the grconvertX and grconvertY functions. 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)
    
    0 讨论(0)
提交回复
热议问题