Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to remove all margins so that plot region comprises the entire graphic

Tags:

plot

r

I am trying to remove all margins and the "figure region" of a plot in R, so that the plot region comprises the entire graphic device. I thought the code below would do it, but there is still a border around my plot (wider on left/bottom, thinner on top/right). Thanks

par(oma=c(0, 0, 0, 0))
par(mar=c(0, 0, 0, 0))
par(plt=c(0, 1, 0, 1))

Thought I would add a picture to show my progress. The xaxs and yaxs removed nearly all border from the top and right- there is still a border on the left and bottom.

R plot

The relevant portion of my script is below.

png("Test.png", 
     width = 256, height = 256,
     units = "px", pointsize = 6.4, 
     bg = "black", res = NA)

par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')


smoothScatter(lhb$px, lhb$pz, nrpoints=0, xlim=c(-3,3), ylim=c(0,5), 
    main="", xlab="", ylab="", axes=FALSE, 
    colramp=colorRampPalette(c("black", "#202020", "#736AFF", "cyan", "yellow", "#F87431", "#FF7F00", "red", "#7E2217"))
    )

segments(.83, 1.597, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, -.83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 3.436, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, .83, 1.597, col = par("fg"), lty = par("lty"), lwd = par("lwd"))


dev.off()
like image 697
taglius Avatar asked Apr 14 '11 13:04

taglius


1 Answers

One issue is fundamentally not getting what plt does. From ?par we have:

 ‘plt’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the
      coordinates of the plot region as fractions of the current
      figure region.

So your plot region is of zero size if you do par(plt=c(1, 1, 1, 1)), so that doesn't seem to be the way to go. This is because the figure region contains the plot region.

This plot seems to cover the entire region, without any margins:

op <- par(mar = rep(0, 4))
plot(1:10)
par(op)

it covers it so well you can't see the axes or the box:

full region covered

This assumes the default for 0 outer margin (oma). Is this what you were looking for?

We can see that just adjusting the plot margins, as above, we also change the plt parameter as a side effect:

> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467
> op <- par(mar = rep(0, 4))
> par("plt")
[1] 0 1 0 1
> par(op)
> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467

indicating that simply setting the plot margins is sufficient to get a plot/figure region encompassing the entire device.

Of course, there is still a bit of internal padding that insures the ranges of the axes are slightly large than the range of the data in both the x and y coordinates. But you can control this with xaxs and yaxs --- see ?par

Update: As the OP has shown the sort of figure they are trying to produce without margins, I can provide a reproducible example:

set.seed(1)
dat <- matrix(rnorm(100*100), ncol = 100, nrow = 100)

layout(matrix(1:2, ncol = 2))
image(dat)
op <- par(mar = rep(0, 4))
image(dat)
par(op)
layout(1)

which gives for comparison:

comparison of default and no margins respectively

and showing just the full plotting region:

full plot region covered

like image 132
Gavin Simpson Avatar answered Sep 26 '22 05:09

Gavin Simpson