我有一个shapefile(SpatialLinesDataFrame)包含科隆的所有街道,可以从here下载。我将这个@data与来自外部源的数据合并。我如何绘制这些街道(如果可能的话,在谷歌地图上使用ggmaps),以便每个街道都有不同的颜色(或厚度),具体取决于其个人价值?在这一点上我的另一列添加到SHP @数据的数据帧,其中包含每个街道一定值如何使用ggplot/ggmap在SpatialLinesDataFrame中绘制和着色街道?
shapefile <- readOGR(shapfile, "Strasse", stringsAsFactors=FALSE,
encoding="latin-9")
shp <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84"))
:
到目前为止,我已经做到了这一点。然后我fortifiy的shape文件,因此它可以使用ggplot绘制:
shp$id <- rownames([email protected])
shp.df <- as.data.frame(shp)
data_fort <- fortify(shp, region = "id")
data_merged <- join(data_fort, shp.df, by="id")
当我使用geom_lines,该行不看好,并且不容易识别:
ggplot(data_merged, aes(x=long, y=lat,
group=group,
colour=values)) +
geom_line()
Here我看到人们可以转换shapefile,以便geom_segement(或者在这种情况下是一个修改后的函数“geom_segment2”)可以被使用,但是会丢失我的街道特定值。
你的shapefile似乎有> 5500条街道。你想让他们每个人都变成另一种颜色吗?另外,属性表没有'values'列。 – jlhoward
我将@data与外部数据文件(我无法上传,因为隐私原因)相匹配。只有那些街道仍留在shapefile中,这些街道在我的原始数据文件中。然后我将外部文件中的值附加到shapefile @ data数据框。现在我意识到我可以上传修改后的shapefile文件,明天就可以做到这一点。值列在我的shapefile中,它包含从1-10开始的值,但是这是模拟的。当我使用geom_line()时,某些线条(街道)的某些部分充满了形状等颜色,你知道这是为什么吗? –
对于初学者,您需要使用'geom_path(...)',而不是'geom_line(...)。 – jlhoward