Skip to content

Instantly share code, notes, and snippets.

@Mbrownshoes
Last active February 21, 2018 20:06
Show Gist options
  • Save Mbrownshoes/82ea3e2377106d1f70b9af62a29450ee to your computer and use it in GitHub Desktop.
Save Mbrownshoes/82ea3e2377106d1f70b9af62a29450ee to your computer and use it in GitHub Desktop.
library(dplyr)
library(sf)
library(sp)
library(RColorBrewer)
library(rmapshaper)
#### communities
cm <- st_read("CR/communities/Communities_CR.shp")
# st_crs(cm) <- p4s
cm<-st_transform(cm, 3005)
## converting sf to sp and then to df
cm <- as(cm, "Spatial")
cm.df <- as.data.frame(coordinates(cm))
communities <- data.frame(id = cm$NAME, cm.df) %>%
filter(id != 'Kamloops')
names(communities) <- c("Name","Longitude", "Latitude") #more sensible column names
## Use for cities, but they're locations are odd
# communities <- data.frame(id = cm$PLACE, cm.df) %>%
# filter(id != 'Duncan', id != 'Richmond', id != 'Burnaby', id != 'New Westminster', id != 'Surrey')
# names(communities) <- c("Name","Longitude", "Latitude") #more sensible column names
#### load US
us <- st_read("CR/cb_2014_us_nation_5m/cb_2014_us_nation_5m.shp")
us_simp <- st_transform(us,3005)
us_simp <- ms_simplify(us_simp)
### Ocean
ocean50 <-st_read("CR/ne_10m_ocean/ne_10m_ocean.shp")
ocean <- ocean50 %>%
st_transform(3005) %>%
as("Spatial")
ocean <- fortify(ocean)
#### college regions
nc <- st_read("CR/CR_2012.shp")
cr_simp <- ms_simplify(nc)
# add projection
p4s <-"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
st_crs(cr_simp) <- p4s
# BC Albers projection
cr_simp<-st_transform(cr_simp, 3005)
## converting sf to sp and then to df
plotmap <- as(cr_simp, "Spatial")
plotmapdf <- fortify(plotmap, region = "CR_NAME") %>%
mutate(VI = ifelse(id== 'Vancouver Island','Vancouver Island','other'))
us <- as(us_simp,"Spatial")%>%
fortify(region ="NAME" )
#Get centroids of regions for labels
centroids.df <- as.data.frame(coordinates(plotmap))
names(centroids.df) <- c("Longitude", "Latitude") #more sensible column names
idList <- plotmap@data$CR_NAME
places <- data.frame(id = idList, centroids.df)
## plotting chloropleth
pal <- c("#FED98E","grey50","#FFFFD4",'lightblue')
ggplot(data = plotmapdf, aes(x = long, y = lat, group = group,fill=VI)) +
geom_polygon(data = ocean, aes(long, lat, group = group,fill=hole),inherit.aes = FALSE) +
geom_polygon(alpha = 0.9) +
geom_path(colour = "grey50", size = 0.3) +
coord_fixed(xlim = c(1370571.8, 1003875.7), ylim = c(369045, 649045), ratio = 0.79) +
scale_fill_manual(values = rev(pal))+
guides(fill=FALSE) +
geom_text(data = places,aes(label = id, x = Longitude, y = Latitude),size=3.5,color='red',inherit.aes = FALSE) +
geom_text(data = communities,aes(label = Name, x = Longitude, y = Latitude),size = 2.5,inherit.aes = FALSE) +
labs(title = "Vancouver Island \n College Region") +
theme_minimal() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
# legend.title = element_text(size = 14, face = "bold"),
# legend.text = element_text(size = 13),
# legend.position = c(0.15, 0.2),
text = element_text(family = "Verdana"),
plot.title = element_text(vjust=-13,hjust = .99,size = 12),
plot.margin = unit(c(5, 5, 5, 5), "mm"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment