Created
September 22, 2019 06:46
-
-
Save danthemango/f13317fb4f941b54d55e49d5b893797f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# builds a roadmap of Vancouver, coloured by road suffix | |
# most of the code taken from github.com/erdavis1/RoadColors | |
# install the packages, you only need to do this once. | |
install.packages(c('sf', 'foreign', 'tidyverse', 'lwgeom')) | |
# load in the necessary libraries | |
library(sf) | |
library(foreign) | |
library(tidyverse) | |
library(lwgeom) | |
options(stringsAsFactors = FALSE) | |
# set the working directory | |
# setwd("input/") | |
# .shp file (without suffix) | |
# taken from Road Network File - 2010 - British Columbia | |
# https://open.canada.ca/data/en/dataset/143d136f-3b24-408a-8188-e979cb775852 | |
filename <- "grnf059r10a_e" | |
# I used latlon.com to pick a bounding box around the city | |
df<- read.table(text ="lat lon | |
49.397373 -123.315913 | |
49.397373 -122.629007 | |
49.036419 -122.629007 | |
49.036419 -123.315913 | |
49.397373 -123.315913",header= T) | |
#pick the road types you'd like to have colored in the plot | |
plottypes <- c('HWY', 'RD', 'AVE', 'ST', 'DR', 'CRES', 'TRAIL', 'LINE') | |
#set colors for each road type | |
plotcolors <- c('HWY' = '#FE4D64', 'RD' = '#4cb580', 'AVE' ='#59c8e5', 'ST' = '#fed032', 'DR' = '#a7abfe', | |
'CRES' = '#fe9ea5', 'TRAIL' = '#2e968c', 'LINE' = '#ff9223', 'Other' = '#cccccc') | |
#import road geography | |
allroads <- read_sf(".", filename) | |
allroads$len <- st_length(allroads) | |
polygon <- df %>% | |
st_as_sf(coords = c("lon", "lat"), crs = 4326) %>% | |
st_transform(32195) %>% | |
summarise(geometry = st_combine(geometry)) %>% | |
st_cast("POLYGON") | |
#subset the roads into a sub-boundary. You can remove this if you want to plot the entire province. | |
boundary <- polygon %>% st_transform(st_crs(allroads)) | |
allroads <- st_intersection(boundary, allroads) | |
#put other roads into their own dataframe | |
allroads$TYPE[!(allroads$TYPE %in% plottypes)] <- "Other" | |
otherroads <- allroads[(allroads$TYPE == "Other"),] | |
allroads <- allroads[(allroads$TYPE != "Other"),] | |
#plot it | |
blankbg <-theme(axis.line=element_blank(),axis.text.x=element_blank(), | |
axis.text.y=element_blank(),axis.ticks=element_blank(), | |
axis.title.x=element_blank(), axis.title.y=element_blank(), | |
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(), | |
panel.grid.minor=element_blank(),plot.background=element_blank()) | |
#depending on the city/radius you're mapping you might need to adjust the size variables below. | |
#I just do trial/error till I like the way it looks when it's saved | |
ggplot() + blankbg + theme(panel.grid.major = element_line(colour = "transparent")) + | |
geom_sf(data=otherroads, size = .5, aes(color=TYPE)) + | |
geom_sf(data=allroads, size = .5, aes(color=TYPE)) + | |
scale_color_manual(values = plotcolors, guide = FALSE) | |
ggsave("myplot.png", plot = last_plot(), scale = 1, width = 18, height = 36, units = "in", dpi = 500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview of the result