Skip to content

Instantly share code, notes, and snippets.

@rexarski
Created March 15, 2021 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexarski/fc8c553824d6a310dde29a698f3d47f7 to your computer and use it in GitHub Desktop.
Save rexarski/fc8c553824d6a310dde29a698f3d47f7 to your computer and use it in GitHub Desktop.
personal art map
# source
# https://ggplot2tutor.com/streetmaps/streetmaps/
# http://estebanmoro.org/post/2020-10-19-personal-art-map-with-r/
# create the streetmap
pacman::p_load(osmdata, ggplot2)
# bbx <- getbb("Boston, MA")
bbx <- getbb("Canberra, Australia")
min_lon <- bbx[1,1]
max_lon <- bbx[1,2]
min_lat <- bbx[2,1]
max_lat <- bbx[2,2]
# sometimes a more specifc area
# min_lon <- x; max_lon <- y
# min_lat <- z; max_lat <- m
# bbx <- rbind(x=c(min_lon, max_lon), y=c(min_lat, max_lat))
# colnames(box) <- c("min", "max")
# OSM data has many layers/features, see something related to "highway"
available_tags("highway")
# pick out some
highways <- bbx %>%
opq() %>%
add_osm_feature(key = "highway",
value = c("motorway", "trunk",
"primary","secondary",
"tertiary","motorway_link",
"trunk_link","primary_link",
"secondary_link",
"tertiary_link")) %>%
osmdata_sf()
# look at the lines:
require(sf) # hey what is sf?
ggplot() +
geom_sf(data = highways$osm_lines,
aes(color=highway),
size = .4,
alpha = .65) +
theme_void()
streets <- bbx %>%
opq()%>%
add_osm_feature(key = "highway",
value = c("residential", "living_street",
"service","unclassified",
"pedestrian", "footway",
"track","path")) %>%
osmdata_sf()
ggplot() +
geom_sf(data = streets$osm_lines,
aes(color=highway),
size = .4,
alpha = .65)+
theme_void()
# combine the two above, add different width for different road types, crop streets out of box
# color_roads <- rgb(0.42,0.449,0.488)
color_roads <- "#a8dadc"
color_background <- "#1d3557"
p <- ggplot() +
geom_sf(data = streets$osm_lines,
col = color_roads,
size = .2,
alpha = .35) +
geom_sf(data = highways$osm_lines,
col = color_roads,
size = .5,
alpha = .7)+
coord_sf(xlim = c(min_lon,max_lon),
ylim = c(min_lat,max_lat),
expand = FALSE)+
theme(legend.position = F) + theme_void() +
theme(panel.background=
element_rect(fill = color_background))
ggsave(plot = p, "map-canberra.png")
# this looks really really nice.
# adding the geography, like the land, rivers, coastlines
# use polygons instead of raster images
# {tigris} extracts boundaries in ESRI's shapefile format, provided by the US Census
require(tigris)
counties_MA <- counties(state="MA",cb=T,class="sf",)
counties_MA <- st_crop(counties_MA,
xmin=min_lon,xmax=max_lon,
ymin=min_lat,ymax=max_lat)
ggplot() +
geom_sf(data=counties_MA,fill="gray",lwd=0)+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()
get_water <- function(county_GEOID){
area_water("MA", county_GEOID, class = "sf")
}
water <- do.call(rbind,
lapply(counties_MA$COUNTYFP,get_water))
water <- st_crop(water,
xmin=min_lon,xmax=max_lon,
ymin=min_lat,ymax=max_lat)
ggplot() +
geom_sf(data=counties_MA)+
geom_sf(data=water,
inherit.aes = F,
col="red")+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()
st_erase <- function(x, y) {
st_difference(x, st_union(y))
}
counties_MA <- st_erase(counties_MA,water)
ggplot() +
geom_sf(data=counties_MA,
lwd=0)+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()
ggplot() +
geom_sf(data=counties_MA,
inherit.aes= FALSE,
lwd=0.0,fill=rgb(0.203,0.234,0.277))+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()+
theme(panel.background=
element_rect(fill = rgb(0.92,0.679,0.105)))+
ggtitle("Dark + Yellow theme")
ggplot() +
geom_sf(data=counties_MA,
inherit.aes= FALSE,
lwd=0.0,fill="white")+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()+
theme(panel.background=
element_rect(fill = rgb(0.9,0.9,0.9)))+
ggtitle("Black + White theme")
ggplot() +
geom_sf(data=counties_MA,
inherit.aes= FALSE,
lwd=0.0,fill=rgb(0.95,0.95,0.95))+
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE)+
theme(legend.position = F) + theme_void()+
theme(panel.background=element_rect(fill = "black"))+
ggtitle("White + Black theme")
ggplot() +
geom_sf(data=counties_MA,
inherit.aes= FALSE,
lwd=0.0,fill=rgb(0.203,0.234,0.277))+
geom_sf(data = streets$osm_lines,
inherit.aes = FALSE,
color=color_roads,
size = .4,
alpha = .65) +
geom_sf(data = highways$osm_lines,
inherit.aes = FALSE,
color=color_roads,
size = .6,
alpha = .65) +
coord_sf(xlim = c(min(bbx[1,]), max(bbx[1,])),
ylim = c(min(bbx[2,]), max(bbx[2,])),
expand = FALSE) +
theme(legend.position = F) + theme_void()+
theme(panel.background=
element_rect(fill = rgb(0.92,0.679,0.105)))
@rexarski
Copy link
Author

canberra-2021

@hodanli
Copy link

hodanli commented Oct 19, 2021

hey. thanks for sharing this. can you share the last version of the script. i cannot recreate the latest image. thanks in advance.

@rexarski
Copy link
Author

rexarski commented Oct 20, 2021

hey. thanks for sharing this. can you share the last version of the script. i cannot recreate the latest image. thanks in advance.

@hodanli

Hey, everything above the call of {tigris} still works fine. The reason why it raises errors (probably warnings actually) is that {tigris} extracts boundaries in ESRI's shapefile format, which is originally provided by the US Census Bureau. In other words, the shapefile wouldn't actually fit in to draw the shapes of any places outside the States.

As for the img above, I made some modifications based on the ggplot output with Figma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment