Last active
November 12, 2018 13:50
-
-
Save hrbrmstr/e3d0dc87eaacf7bbece7 to your computer and use it in GitHub Desktop.
Swiss Cantons - R version of http://bl.ocks.org/mbostock/4207744
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
library(rgeos) | |
library(rgdal) # needs gdal > 1.11.0 | |
library(ggplot2) | |
# map theme | |
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df") | |
map = readOGR("readme-swiss.json", "cantons") | |
map_df <- fortify(map) | |
# create mapping for id # to name since "region=" won't work | |
dat <- data.frame(id=0:(length(map@data$name)-1), canton=map@data$name) | |
map_df <- merge(map_df, dat, by="id") | |
# find canton centers | |
centers <- data.frame(gCentroid(map, byid=TRUE)) | |
centers$canton <- dat$canton | |
# make a map! | |
gg <- ggplot() | |
gg <- gg + geom_map(data=map_df, map=map_df, | |
aes(map_id=id, x=long, y=lat, group=group), | |
color="#ffffff", fill="#bbbbbb", size=0.25) | |
# gg <- gg + geom_point(data=centers, aes(x=x, y=y)) | |
gg <- gg + geom_text(data=centers, aes(label=canton, x=x, y=y), size=3) | |
gg <- gg + coord_map() | |
gg <- gg + labs(x="", y="", title="Swiss Cantons") | |
gg <- gg + theme_map() | |
gg | |
ggsave("cantons.svg", gg, width=9, height=6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just a reminder: plotting the data in
readme-swiss.json
doesn't correctly display the cantons AI and AR because they get "overwritten" by SG. you have to reorder the data to correct this:Additionally I'd recommend to remove all the duplicate rows to tremendously speed up plotting :)my fault, there are no duplicates in thereadme-swiss.json
; instead I've happened to produce them by myself 🙈