3

I am trying to plot a map of the Arctic in Polar stereographic projection. To do this I have imported an already projected shapefile into R using the rGDAL package:

ds <- readOGR(dsn = path, layer = "10m_coastline_ps")

This creates a SpatialLinesDataFrame. Now when I plot this SpatialLinesDataFrame using the plot() function, I am unable to set the plot limits using the xlim and ylim parameters:

plot(ds, xlim=c(-1700000,1700000), ylim=c(-4000000,800000), axes=T)

The plot() function automatically sets the aspect ratio to 1 and forces the axis limits (as explained in this post: igraph axes xlim ylim plot incorrectly). This produces the following plot:

enter image description here

I have tried initializing the plot extent before plotting the SpatialLinesDataFrame as shown below:

mapExtentPr <- rbind(c(-1700000,-4000000), c(1700000,800000))
plot(mapExtentPr, pch=NA)
plot(ds, xlim=c(-1700000,1700000), ylim=c(-4000000,800000), axes=T, asp = 1, add=TRUE)

This sets the right limits but does not maintain the aspect ratio. I have also tried using the clip() function which had no effect.

Is there a way to set the plot limits exactly while maintaining an aspect ratio of 1 (without using ggplot or spplot)?

Note: I have checked this answer already: Is it possible to change the ylim and xlim when the plot has already been drawn?

Community
  • 1
  • 1
jatobat
  • 749
  • 3
  • 8
  • 21
  • 1
    Does adding `pty = "s"` to either the full `plot` call from the first example, or the `plot(mapExtentPr, pch=NA)` call from the second example help? The point here is to maintain a square plotting region otherwise it is impossible to force 1asp = 1 and control the axis limits. Even when the plotting region is square, you won't be able to force it perfectly. The other option is to rescale the plotting window until you get roughly the limits you want. – Gavin Simpson Mar 26 '13 at 16:32
  • Thanks for the suggestions. Unfortunately, adding `pty = "s"` doesn't do the job since I will be needing a non square region. Rescaling the plotting window works fine, but I was looking for an automatic procedure. I guess I will probably end up doing this with `ggplot` or `spplot`... – jatobat Mar 26 '13 at 16:59
  • 1
    If you want an aspect ratio of 1 *and* specified limits, you'll need to set the dimensions of the plot. How else would this be possible? You can open a new device with a specific size in base R. If **ggplot** and **spplot** can do this then perhaps you could explain how in an Answer? You can answer your own Q in a day or so. – Gavin Simpson Mar 26 '13 at 17:05

1 Answers1

3

I ended up using this:

xmin <- -1700000
xmax <- 1700000
ymin <- -4000000
ymax <- 100000

asratio = (ymax-ymin)/(xmax-xmin)

png("greenland_map.png",width=500,height=ceiling(500*asratio))

#plot basemap
plot(ne_10m_coastline_ps,xlim=c(xmin,xmax), ylim=c(ymin,ymax),
     axes=TRUE,asp=1,xaxs="i",yaxs="i")

par(usr=c(xmin,xmax,ymin,ymax))

dev.off()

map with exact axis limits

jatobat
  • 749
  • 3
  • 8
  • 21