如何使用ggplot/ggmap在SpatialLinesDataFrame中绘制和着色街道?

如何使用ggplot/ggmap在SpatialLinesDataFrame中绘制和着色街道?,r,plot,ggplot2,spatial,ggmap,R,Plot,Ggplot2,Spatial,Ggmap,我有一个包含科隆所有街道的shapefile(SpatialLinesDataFrame),可以从下载。我将这个@数据与来自外部源的数据合并。我如何绘制这些街道(如果可能的话,在谷歌地图上使用ggmaps),使每一条街道都有不同的颜色(或厚度),这取决于它各自的值 到目前为止,我已经做到了: shapefile <- readOGR(shapfile, "Strasse", stringsAsFactors=FALSE, encoding="latin-9") shp <- spT

我有一个包含科隆所有街道的shapefile(SpatialLinesDataFrame),可以从下载。我将这个@数据与来自外部源的数据合并。我如何绘制这些街道(如果可能的话,在谷歌地图上使用ggmaps),使每一条街道都有不同的颜色(或厚度),这取决于它各自的值

到目前为止,我已经做到了:

shapefile <- readOGR(shapfile, "Strasse", stringsAsFactors=FALSE, 
encoding="latin-9")
shp <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84"))

我看到可以转换shapefile,以便使用geom_Segment(或者在本例中是一个修改的函数“geom_segment2”),但随后将丢失特定于街道的值。

因此,此代码从shapefile中获取100条最长的道路,在(1,10)上随机分配“值”,并根据值使用颜色绘制,在谷歌的科隆光栅图像上

library(ggplot2)   
library(ggmap)          # for ggmap(...) and get_map(...)
library(rgdal)          # for readOGR(...)
library(plyr)           # for join(...)
set.seed(1)             # for reproducible example
setwd(" <directory with your shapefiles> ")
spl <- readOGR(dsn=".", "Strasse", encoding="latin-9")
spl <- spl[spl$SHAPE_LEN %in% tail(sort(spl$SHAPE_LEN),100),]
shp       <- spTransform(spl, CRS("+proj=longlat +datum=WGS84"))
shp.df    <- data.frame(id=rownames(shp@data),
                        values=sample(1:10,length(shp),replace=T),
                        shp@data, stringsAsFactors=F)

data_fort   <- fortify(shp)
data_merged <- join(data_fort, shp.df, by="id")

ggmap(get_map(unlist(geocode("Cologne")),zoom=11))+
  geom_path(data=data_merged,size=1,
            aes(x=long,y=lat,group=group,color=factor(values)))+
  labs(x="",y="")+
  theme(axis.text=element_blank(),axis.ticks=element_blank())

但是有一个问题:
zoom=…
参数的解释不同,我无法充分缩放地图。

您的shapefile似乎有>5500条街道。你想让每一个都是不同的颜色吗?此外,attributes表没有
values
列。我将@data与外部数据文件进行了匹配(出于隐私原因,我无法上载)。只有那些街道仍然保留在shapefile中,即我的原始数据文件中。然后我将外部文件中的值附加到shapefile@data数据帧。现在我意识到我可以上传修改过的shapefile,明天我会上传的。值列在我的shapefile中,例如它包含1-10的值,但这是模拟的。当我使用geom_line()时,某些线条(街道)的某些部分充满了类似颜色的形状,你知道这是为什么吗?对于初学者,你需要使用
geom_path(…)
,而不是“geom_line(…)。只是在没有真实数据的情况下对其进行了测试,看起来好多了:)谢谢。我还发现,用户不需要直接访问@data frame,而是通过shapefile对象。
ggplot(data_merged, aes(x=long, y=lat,
                      group=group,
                      colour=values)) +
 geom_line()
library(ggplot2)   
library(ggmap)          # for ggmap(...) and get_map(...)
library(rgdal)          # for readOGR(...)
library(plyr)           # for join(...)
set.seed(1)             # for reproducible example
setwd(" <directory with your shapefiles> ")
spl <- readOGR(dsn=".", "Strasse", encoding="latin-9")
spl <- spl[spl$SHAPE_LEN %in% tail(sort(spl$SHAPE_LEN),100),]
shp       <- spTransform(spl, CRS("+proj=longlat +datum=WGS84"))
shp.df    <- data.frame(id=rownames(shp@data),
                        values=sample(1:10,length(shp),replace=T),
                        shp@data, stringsAsFactors=F)

data_fort   <- fortify(shp)
data_merged <- join(data_fort, shp.df, by="id")

ggmap(get_map(unlist(geocode("Cologne")),zoom=11))+
  geom_path(data=data_merged,size=1,
            aes(x=long,y=lat,group=group,color=factor(values)))+
  labs(x="",y="")+
  theme(axis.text=element_blank(),axis.ticks=element_blank())
ggmap(get_map("Cologne"))