How to plot and colour streets in a SpatialLinesDataFrame with ggplot/ggmap?
Asked Answered
M

1

6

I've got a shapefile (SpatialLinesDataFrame) containing all streets in cologne, which can be downloaded from here. I merged this @data with data from an external source. How can i plot these streets (if possible on an google map using ggmaps), so that every street has a different colour (or thickness), depending on its individual value?

So far, i have done this:

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

at this point i add another column to the shp@data data frame, which contains a certain value for each street. Then I fortifiy the the shapefile so it can be plotted using ggplot:

shp$id <- rownames(shp@data)
shp.df <- as.data.frame(shp)
data_fort <- fortify(shp, region = "id")
data_merged <- join(data_fort, shp.df, by="id")

When i use geom_lines, the lines do not look good and are not easy to identify:

ggplot(data_merged, aes(x=long, y=lat,
                      group=group,
                      colour=values)) +
 geom_line()

Here i saw that one could transform the shapefile so that geom_segement (or in this case a modified function "geom_segment2") can be used, but then would loose my the street specific values.

Mihalco answered 15/7, 2014 at 14:35 Comment(4)
Your shapefile seems to have >5500 streets. Do you want every one of them to be a different color? Also, the attributes table does not have a values column.Maida
I matched the @data with an external data file (which i can't upload for privacy reasons). Only those streets are still left in the shapefile, that are in my original data file. Then i append the values from the external file to the shapefile@data dataframe. Right now i realize i can upload the modified shapefile, will do that tomorrow. The values column is in my shapefile, it contains values from 1-10 for instance, but that's simulated. When i use geom_line() some parts of certain lines (streets) are filled with colour like shapes, do you know why that is?Mihalco
For starters, you need to use geom_path(...), not `geom_line(...).Maida
just tested it without real data, looks way better :) thanks. also i found out, that one does not need to access the @data frame directly, but through the shapefile object.Mihalco
M
4

So this code grabs the 100 longest roads from your shapefile, randomly assigns "values" on (1,10), and plots that with color based on value, on top of a google raster image of Cologne.

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())

It is possible to make the ggmap(...) call simpler using, e.g.,

ggmap(get_map("Cologne"))

but there's a problem: the zoom=... argument is interpreted differently and I wasn't able to zoom the map sufficiently.

Maida answered 15/7, 2014 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.