首页 > 解决方案 > 如何在基地R中绘制带有南中国海的中国地图

问题描述

我想得到一张以南海框架为附件的中国地图。中国边界线和行政形状文件可以从 https://github.com/jimrpy/jimrpy.github.io/blob/master/epidemiology/Archive.zip下载。

我的代码如下,不知道如何设置“usr”,如何修改代码?

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)

在此处输入图像描述

标签: rgis

解决方案


graphics 参数定义了您想要绘制的坐标系统的usr区域,而不是您想要绘制的画布区域。该区域由plt图形参数控制。

有关详细信息,请参阅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.
(...)

您可以通过调用来查看当前的参数集par()(或者,如果您只想查看特定参数,par(c("plt","usr")).

在您的情况下,您似乎想要xleft = 107, ybottom = 0, xright = 122, ytop = 21在插图中绘制矩形 ( ) 给出的范围,因此您需要usr使用这些值进行定义。关于您想要绘图的画布区域,您可能需要进行一些实验。c(0.76, 0.935, 0.195, 0.45)对我来说效果很好,但我猜这可能取决于各种设置,对你来说可能会有所不同。

无论如何,尝试这样的事情:

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

推荐阅读