Rodrigo
Rodrigo

Reputation: 5129

Plot in R out of proportion. Why?

I want to plot a world map in R, but for some reason there are unnecessary margins at the top and bottom of the image. I reduced the code to its minimal.

png('mundi.png',1000,500)
par(mar=c(0,0,0,0),bg='green')
plot(NA,xlim=c(-180,180),ylim=c(-90,90),xaxs='i',bty='n',xaxt='n',yaxt='n',asp=1)
par('usr')
rect(-180,-90,180,90,col='white')
dev.off()

The generated image is

rectangle 1000x500

The green areas are the unnecessary margins. The dimension of both the image and the coordinates are 2x1 (1000x500 and 360x180, respectively). xaxs='i' should make exact coordinates. asp=1 should not be needed, and is not making any difference. par('usr') is returning

-180.0  180.0  -97.2   97.2

Why isn't it returning

-180.0  180.0  -90.0   90.0

As it (supposedly) should?

Upvotes: 1

Views: 60

Answers (1)

RoB
RoB

Reputation: 1984

You were almost there, actually. To control the limits of the plot window through xlim/ylim without the extra space R tends to add, you have to specify the x/yaxt = 'i' option. You set it for the x axis, but not the y axis.

When I do set it for the y axis, the values outputted are as expected, and the plot is completely white (forgive me for not putting up an image of it ;-) )

png('mundi.png', width = 1000, height = 500)
par(mar=c(0,0,0,0), bg='green')
plot(NA, xlim = c(-180,180), ylim = c(-90,90), 
     xaxs='i', yaxs='i', # set both axes to 'i' option
     bty='n',xaxt='n',yaxt='n',asp=1)
par('usr')
# [1] -180  180  -90   90
rect(-180,-90,180,90,col='white')
dev.off()

Upvotes: 1

Related Questions