【问题标题】:Plotting Cities on a GADM map with R用 R 在 GADM 地图上绘制城市
【发布时间】:2015-07-13 08:11:23
【问题描述】:

我正在尝试使用 GADM 数据并使用 R 绘制印度各州/地区的详细地图。我使用了以下代码

# Load required libraries
library(sp)
library(RColorBrewer)
# ---------------------------------------------------------------------------
# load level 2 india data downloaded from http://gadm.org/country
load("IND_adm2.RData")
ind2 = gadm
# plotting districts of a State, in this case West Bengal
wb2 = (ind2[ind2$NAME_1=="West Bengal",])
spplot(wb2,"NAME_1", main = "West Bengal Districts",
       colorkey=F, scales=list(draw=T))

生成这张地图


我现在尝试添加几个标记(或点加文本)来显示地区总部,“Purulia”,位于 lon = 86.36521 lat = 23.33208,但不知何故我无法正确使用 sp.layout 语法。对于一组已知其长、纬度的城镇,我将需要它。如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 不清楚你为什么使用 spplot 而不是 plot。请提供一个可重现的例子,例如使用让您下载 gadm 数据的命令,以及创建 Purulia 的命令。

标签: r sp


【解决方案1】:

这是一个简短的例子:

library("sp")

# load level 2 india data from gadm.org
library("raster")
ind2 <- getData('GADM', country='IND', level=2)
wb2 <- ind2[ind2$NAME_1=="West Bengal",]

cities <- data.frame(name="Purulia", lon=86.36521, lat=23.33208)

spplot(wb2, "NAME_1",
       sp.layout=list("panel.points", cities$lon, cities$lat, col="red"),
       main="West Bengal Districts",
       colorkey=FALSE, scales=list(draw=TRUE))

或者如果您的城市位于SpatialPointsDataFrame

cities <- data.frame(name="Purulia", lon=86.36521, lat=23.33208)
coordinates(cities) <- ~ lon + lat
class(cities)
# [1] "SpatialPointsDataFrame"

spplot(wb2, "NAME_1",
       sp.layout=list("sp.points", cities, col="red"),
       main="West Bengal Districts",
       colorkey=FALSE, scales=list(draw=TRUE))

【讨论】:

  • 这个解决方案效果很好。现在我需要看看如何将城市名称放在标记旁边,但这可能会使地图非常笨拙。非常感谢提供的解决方案
【解决方案2】:

以下是如何使用基本图(而不是 spplot / levelplot)来做到这一点

library(raster)
# get the data 
ind2 <- getData('GADM', country='IND', level=2)
wb2 <- ind2[ind2$NAME_1=="West Bengal",]
cities <- data.frame(name="Purulia", lon=86.36521, lat=23.33208)

# plot
plot(wb2, border='gray', col='light gray')
points(cities[, 2:3], col='red', pch=20)
text(cities[, 2:3], labels= cities[,1], pos=4)

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 2011-12-31
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多