Friday, November 26, 2010

Remove inner margins in R plots

You may notice that xlim and ylim options in R plots do not make the horizontal and vertical axes start and end at your specified values. Instead, by default the specified ranges are enlarged by 6%, so that the specified values do not lie at the very edges of the plot region. This is appropriate for most types of plot, but sometimes we want the specified limits to lie at the edges of the plot window. This can be specified separately for each axis using the arguments xaxs and yaxs. Please refer to the following example and pay attention to the difference of corners in the two plots.

Here is the help document on xaxs and yaxs in R.

xaxs

The style of axis interval calculation to be used for the x-axis. Possible values are "r", "i", "e", "s", "d". The styles are generally controlled by the range of data or xlim, if given. Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range. Style "i" (internal) just finds an axis with pretty labels that fits within the original data range. Style "s" (standard) finds an axis with pretty labels within which the original data range fits. Style "e" (extended) is like style "s", except that it is also ensures that there is room for plotting symbols within the bounding box. Style "d" (direct) specifies that the current axis should be used on subsequent plots. (Only "r" and "i" styles are currently implemented)

> x <- rnorm(100)
> y <- rnorm(x)
> plot(x, y, xlim = c(-2, 2), ylim = c(-2, 2))
> plot(x, y, xlim = c(-2, 2), ylim = c(-2, 2), xaxs = "i", yaxs = "i")

No comments:

Post a Comment