使用ggplot2添加要映射的道路

时间:2017-02-18 23:03:09

标签: r ggplot2

如何将道路(或任何交通基础设施)添加到我在R中制作的地图中?我认为这个问题的概括是如何使用geom_polygon()在不使用第二层覆盖第一层的情况下在彼此之上绘制两层?

我特意尝试使用芝加哥病房地图和公交路线图来做到这一点。两者均可在线获取:https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Wards-2015-/sp34-6z76和此处:https://data.cityofchicago.org/Transportation/CTA-Bus-Routes-Shapefile/d5bx-dr8z

分别绘制每个人的代码:

library(ggplot2)
library(rgdal)

wards <- readOGR(dsn=path.expand("~/Desktop/Wards"), layer ="geo_export_d15e486d-2802-4700-afe7-9063b55d3e27")
buses <- readOGR(dsn=path.expand("~/Desktop/CTA_Routes"), layer ="CTA_Routes")

#Fortify Wards
wards.point <- fortify(wards, region = "ward")
colnames(wards.point)[6] <- "ward"
wardsmap.df <- join(wards.point, wards@data, by = "ward")

#Fortify Buses
buses.point <- fortify(buses, by = "OBJECTID")
colnames(buses.point)[5] <- "OBJECTID"
buses.df <- join(buses.point, buses@data, by = "OBJECTID")

#Simple Ward map
ggplot() + geom_polygon(data = wardsmap.df, aes(x = long, y = lat, group = group),
               fill = NA, color = "black", size = 0.25) + theme_nothing(legend = TRUE)

#Simple buses map
ggplot() + geom_polygon(data = buses.df, aes(x = long, y = lat, group = group),
               fill = NA, color = "black", size = 0.25) + theme_nothing(legend = TRUE)

2 个答案:

答案 0 :(得分:1)

所以这就是事情:它会超越情节(或者如你所说的那样覆盖),因为你没有将变量分配给ggplot,所以每次都写入。你需要为ggplot(病房)分配一个,然后覆盖另外两个数据集。

发生了什么,这里的每个对象,ggplot和geom,正在制作地图的一层。如果没有为ggplot分配任何内容,则假设来自第一个geom的变量。这就是为什么它看起来像骑马一样,实际上它只是第二次写第一层。在下面排列的方式中,在基础顶部绘制两层(geom对象)(病房数据)。

这应该很好用。

ggplot(data = wardsmap.df, aes(x = long, y = lat, group = group)) +  
geom_polygon(data = wardsmap.df, aes(x = long, y = lat, group = group),
           fill = NA, color = "black", size = 0.25) +
geom_polygon(data = buses.df, aes(x = long, y = lat, group = group),
           fill = NA, color = "red", size = 0.25) + 
theme_nothing(legend = TRUE)

P.S。如果你把你所有的图层写成color = black,它看起来就像他们不是他们一样,但我认为这只是一个错字,所以我在这里纠正了< / p>

同样,从上面开始改进,因为我看到没有指定坐标:

ggplot(data = wardsmap.df, aes(x = long, y = lat, group = group)) +  
coord_cartesian()+
geom_polygon(data = wardsmap.df, aes(x = long, y = lat, group = group),
           fill = NA, color = "black", size = 0.25) +
geom_polygon(data = buses.df, aes(x = long, y = lat, group = group),
           fill = NA, color = "red", size = 0.25) + 
theme_nothing(legend = TRUE)

答案 1 :(得分:1)

主要问题在于您的数据。它们具有不同的投影,因此它们看起来像是在全球的不同部分。

wards@proj4string
CRS arguments: +proj=longlat +ellps=WGS84 +no_defs

buses@proj4string
CRS arguments:
 +proj=tmerc +lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999 +x_0=300000
+y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0 

一旦我们转换为单个投影,所有都是固定的:

buses <- spTransform(buses, CRS("+proj=longlat +datum=WGS84"))
wards <- spTransform(wards, CRS("+proj=longlat +datum=WGS84"))

在此之后,继续进行:

wards.point <- fortify(wards, region = "ward")
colnames(wards.point)[6] <- "ward"
wardsmap.df <- join(wards.point, wards@data, by = "ward")

#Fortify Buses
buses.point <- fortify(buses, by = "OBJECTID")
colnames(buses.point)[5] <- "OBJECTID"
buses.df <- join(buses.point, buses@data, by = "OBJECTID")

#Simple Ward map
ggplot() + 
  geom_polygon(data = wardsmap.df, aes(x = long, y = lat, group = group), fill = 'black', color = "black", size = 0.25, alpha = .5) + 
  geom_path(data = buses.df, aes(x = long, y = lat, group = group), color = "red", size = 0.35)

对于我对芝加哥的了解,我仍然看起来有些奇怪,但同样可能是我不知道的一些预测因素。这应该足以让你开始

enter image description here