[R] What's box() (exactly) doing?

David Winsemius dwinsemius at comcast.net
Fri Jun 24 04:53:34 CEST 2016


> On Jun 23, 2016, at 7:29 PM, Marius Hofert <marius.hofert at uwaterloo.ca> wrote:
> 
> Hi,
> 
> I would like to replicate the behavior of box() with rect() (don't ask why).
> However, my rect()angles are always too small. I looked a bit into the
> internal C_box but
> couldn't figure out how to solve the problem. Below is a minimal
> working (and a slightly bigger) example.

Are you aware that the range of the plot area is expanded by a factor of something on the order of 1.04 so that data "points" will appear whole? If my memory serves you can call plot with the parameter plot=FALSE and then recover the par(usr) values for the plot area.

-- 
David.
> 
> Cheers,
> Marius
> 
> ## MWE
> plot(NA, type = "n", ann = FALSE, axes = FALSE, xlim = 0:1, ylim = 0:1)
> rect(xleft = 0, ybottom = 0, xright = 1, ytop = 1, col = "grey80") #
> should match box()
> box()
> 
> ## Extended example
> 
> ## Basic plot
> my_rect <- function()
> {
>    plot(NA, type = "n", ann = FALSE, axes = FALSE, xlim = 0:1, ylim = 0:1)
>    rect(xleft = 0, ybottom = 0, xright = 1, ytop = 1, col = "grey80")
> # should match box()
>    box()
> }
> 
> ## Layout
> lay <- matrix(0, nrow = 3, ncol = 3, byrow = TRUE)
> lay[1,1] <- 1
> lay[2,1] <- 2
> lay[2,2] <- 3
> lay[2,3] <- 4
> lay[3,3] <- 5
> layout(lay, heights = c(1, 10, 1), widths = c(10, 1, 10))
> layout.show(5) # => no space between rectangles; calls box() to draw the boxes
> 
> ## Fill layout
> par(oma = rep(0, 4), mar = rep(0, 4))
> my_rect()
> my_rect()
> my_rect()
> my_rect()
> my_rect()
> ## => spaces between rectangles => why?/how to avoid?
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list