【问题标题】:Random lines in map (bug?) with ggmap - how to get rid of them?地图中的随机线(错误?)与ggmap - 如何摆脱它们?
【发布时间】:2015-11-11 15:44:04
【问题描述】:

我在 R (RStudio 0.98) 中使用 ggmap (2.4),并且我有一张带有政治边界的缅甸地图,其中我正在绘制一些语言信息。不幸的是,边框与一些不应该存在的相同颜色的随机线一起显示。一个在北部笔直,另外两个在南部进一步向下延伸到海洋中。我用 2 个红色箭头标记了它们。

这是我的源代码:

library(ggmap)
burma <- get_map(location=c(lon=96, lat=20), maptype='satellite', color='color', zoom=5)
burma.map <- ggmap(burma, darken=c(.33, 'white')) + coord_map(xlim=c(92,102), ylim=c(29,10)) + borders(database="world", colour="yellow")
burma.map

结果是这样的:Myanmar map with borderline syndrom

看起来像国家边界向量中的错误。

【问题讨论】:

  • 你好安德烈,Herzlich Willkommen auf StackOverflow!
  • 丹克谢尔,少校! Ich hoffe, ich mache alles richtig。 ^^

标签: r google-maps maps border ggmap


【解决方案1】:

“内置”世界地图数据存在问题,maps 软件包的新维护者将很快解决这些问题(即,即使有时尝试裁剪多边形也会遇到问题)。在此期间,使用其他更干净的边框并裁剪它们,这样 ggplot2 就不必这样做了(因为一旦将coord_map 扔进去,它自己就不会做好):

library(ggmap)
library(sp)
library(rgeos)
library(raster)
library(maptools)
library(rgdal)
library(ggthemes)

# get better borders

url <- "http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip"
fil <- "tm.zip"
if (!file.exists(fil)) download.file(url, fil)
unzip(fil, exdir="tm")
world_borders <- readOGR("./tm/TM_WORLD_BORDERS_SIMPL-0.3.shp", "TM_WORLD_BORDERS_SIMPL-0.3")

# get the google map first so we know the bounding box

burma <- get_map(location=c(lon=96, lat=20), maptype='satellite', color='color', zoom=5)

# get the bounding box

bb <- as.numeric(attr(burma, "bb"))

# clip the borders to the bounding box

clip_region <- as(extent(bb[2], bb[4], bb[1], bb[3]), "SpatialPolygons")
proj4string(clip_region) <- proj4string(world_borders)
clipped_borders <- fortify(crop(world_borders, clip_region))

# plot the map

burma_map <- ggmap(burma, darken=c(.33, 'white'))
burma_map <- burma_map + geom_map(data=clipped_borders, map=clipped_borders,
                                  aes(x=long, y=lat, map_id=id),
                                  color="yellow", alpha=0)
burma_map <- burma_map + coord_map(xlim=c(92,102), ylim=c(29,10))
burma_map <- burma_map + theme_map()
burma_map

# alternatively use just asia read directly from GeoJSON URL
# NOTE this relies on some of the above code
# GeoJSON Asia extract made with http://geojson-maps.kyd.com.au/
# But you could use any NaturalEarth extract. This is less
# granular than the previous one

just_asia <- readOGR("https://gist.githubusercontent.com/hrbrmstr/94bdd47705d05a50f9cf/raw/0ccc6b926e1aa64448e239ac024f04e518d63954/asia.geojson",
                     "OGRGeoJSON")

clipped_borders_ja <- crop(just_asia, clip_region)
clipped_borders_ja <- fortify(clipped_borders_ja)

burma_map <- ggmap(burma, darken=c(.33, 'white'))
burma_map <- burma_map + geom_map(data=clipped_borders_ja, map=clipped_borders_ja,
                                  aes(x=long, y=lat, map_id=id),
                                  color="yellow", alpha=0)
burma_map <- burma_map + coord_map(xlim=c(92,102), ylim=c(29,10))
burma_map <- burma_map + theme_map()
burma_map

墨卡托是目前使用coord_map 所能获得的最好的。专题制图站点的“锐利”边界也较少。越精确,使用它们所需的时间就越长。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多