使用 fortify () 或 broom::tidy() 在 ggmap 上绘制线形文件,生成类似多边形的输出

Plotting line shapefile on ggmap using fortify () or broom::tidy() producing a polygon-like output

我正在使用 ggmapbroom::tidy 函数将 shapefile 绘制到 googlemap 中以加强它(转换为数据框),但由于某种原因,线 shapefile 显示为google 地图上的多边形。我不知道是什么原因造成的。 可以下载 shapefile here

下面是我的代码:

library(rgdal)
library(rgeos)
library(ggplot2)
library(ggmap)
library(broom)
Route_shape <- readOGR(dsn = "Kaputa-Mporokoso.shp")
  crs(Route_shape) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" 
    myMap <- get_map(location=Route_shape@bbox,
                       source="google", maptype="roadmap", crop=FALSE,colour = class)
      # Reformat shape for mapping purposes
      Route_shape_df <- broom::tidy(Route_shape)
      # Final map figure
      p <- ggmap(myMap) +
        geom_line(data = Route_shape_df, aes(x = long, y = lat, group=group),
                  colour = "red") 
p

我得到以下输出

我设法解决了这个问题 - 希望这能帮助那些在导入线形文件时遇到困难的人,因为我在 SO 的其他地方没有看到它。

geom_line()替换为geom_path()

Route_shape <- readOGR(dsn = "Kaputa-Mporokoso.shp")
  crs(Route_shape) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" 
    myMap <- get_map(location=Route_shape@bbox,
                       source="google", maptype="roadmap", crop=FALSE,colour = class)
      # Reformat shape for mapping purposes
      Route_shape_df <- broom::tidy(Route_shape)
      # Final map figure
      p <- ggmap(myMap) +
        geom_path(data = Route_shape_df, aes(x = long, y = lat, group=group),
                  colour = "red") 
p