Search code examples
rgis

How to plot China map with South China Sea in base R


I want to get a China map with South China Sea frame as the attach. The China boundary line and administrative shapefiles can be download from https://github.com/jimrpy/jimrpy.github.io/blob/master/epidemiology/Archive.zip.

My code is as follows, I don't know how to set the "usr", how to modify the code?

library(maps)
library(rgdal)

china_blank <- readOGR(dsn = "~/China/",
                      layer = "China_Province")
china_line <- readOGR(dsn = "~/China/",
                      layer = "China_Boundary_Nineline")

china_blank <- spTransform(china_blank, CRS("+init=epsg:4326"))
china_line <- spTransform(china_line, CRS("+init=epsg:4326"))

map(china_blank)
map.axes()   
par(usr = c(73, 136, 0, 54))
rect(xleft = 107, ybottom = 0, xright = 122, ytop = 21, col = "white")
map(china_line, xlim = c(108, 122), ylim = c(0, 21), add =T)

enter image description here


Solution

  • The usr graphics parameter defines the area of a system of coordinates that you want plotted, not the area of the canvas where you want the plot. That area is controlled by the plt graphics parameter.

    For details, see the help(par):

    (...)
    ‘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.
    (...)
    ‘usr’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the extremes
          of the user coordinates of the plotting region.  When a
          logarithmic scale is in use (i.e., ‘par("xlog")’ is true, see
          below), then the x-limits will be ‘10 ^ par("usr")[1:2]’.
          Similarly for the y-axis.
    (...)
    

    You can see the current set of parameters by calling par() (or, if you only want to see specific parameters, par(c("plt","usr")).

    In your case, you seem to want to plot the range given by your rectangle (xleft = 107, ybottom = 0, xright = 122, ytop = 21) in the inset, so you will want to define usr using these values. With regard to the area of the canvas where you want the plot you may need to experiment a bit. c(0.76, 0.935, 0.195, 0.45) works nicely for me, but I am guessing that this may depend on various settings and may be different for you.

    In any case, try something like this:

    par(plt = c(0.76, 0.935, 0.195, 0.45))
    par(usr = c(107, 122, 0, 21))