Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active November 12, 2018 13:50
Show Gist options
  • Select an option

  • Save hrbrmstr/e3d0dc87eaacf7bbece7 to your computer and use it in GitHub Desktop.

Select an option

Save hrbrmstr/e3d0dc87eaacf7bbece7 to your computer and use it in GitHub Desktop.

Revisions

  1. hrbrmstr revised this gist Sep 26, 2014. No changes.
  2. hrbrmstr revised this gist Sep 26, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ 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")
  3. hrbrmstr revised this gist Sep 26, 2014. No changes.
  4. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@ library(rgeos)
    library(rgdal) # needs gdal > 1.11.0
    library(ggplot2)

    devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")

    map = readOGR("readme-swiss.json", "cantons")
    map_df <- fortify(map)

    @@ -22,12 +24,7 @@ gg <- gg + geom_map(data=map_df, map=map_df,
    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_bw()
    gg <- gg + theme(panel.grid=element_blank())
    gg <- gg + theme(legend.position="none")
    gg <- gg + theme(panel.border=element_blank())
    gg <- gg + theme(axis.ticks=element_blank())
    gg <- gg + theme(axis.text=element_blank())
    gg <- gg + theme_map()
    gg

    ggsave("cantons.svg", gg, width=9, height=6)
  5. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ library(rgeos)
    library(rgdal) # needs gdal > 1.11.0
    library(ggplot2)

    map = readOGR("/Users/bob/Desktop/readme-swiss.json", "cantons")
    map = readOGR("readme-swiss.json", "cantons")
    map_df <- fortify(map)

    # create mapping for id # to name since "region=" won't work
    @@ -30,4 +30,4 @@ gg <- gg + theme(axis.ticks=element_blank())
    gg <- gg + theme(axis.text=element_blank())
    gg

    ggsave("/Users/bob/Desktop/cantons.svg", gg, width=9, height=6)
    ggsave("cantons.svg", gg, width=9, height=6)
  6. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 5 additions and 16 deletions.
    21 changes: 5 additions & 16 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,19 @@
    library(rvest)
    library(dplyr)
    library(httr) # >0.5
    library(tidyr)
    library(gpclib)
    library(rgeos)
    library(sp)
    library(maptools)
    library(rgdal) # needs gdal > 1.11.0
    library(ggplot2)
    library(reshape2)
    library(gridExtra)

    map = readOGR("readme-swiss.json", "cantons")
    map = readOGR("/Users/bob/Desktop/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)
    dat$color <- "blue"
    dat[2,]$color <- "red"
    dat

    map_df <- merge(map_df, dat, by="id")

    # get and label canton centers
    # 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),
    @@ -41,4 +30,4 @@ gg <- gg + theme(axis.ticks=element_blank())
    gg <- gg + theme(axis.text=element_blank())
    gg

    ggsave("cantons.svg", gg, width=9, height=6)
    ggsave("/Users/bob/Desktop/cantons.svg", gg, width=9, height=6)
  7. hrbrmstr revised this gist Sep 20, 2014. No changes.
  8. hrbrmstr revised this gist Sep 20, 2014. No changes.
  9. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ dat

    map_df <- merge(map_df, dat, by="id")

    # get and label canton centers
    centers <- data.frame(gCentroid(map, byid=TRUE))
    centers$canton <- dat$canton

  10. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cantons.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ![img](http://rud.is/dl/cantons.svg?)
  11. hrbrmstr revised this gist Sep 20, 2014. 1 changed file with 0 additions and 557 deletions.
    557 changes: 0 additions & 557 deletions cantons.svg
    0 additions, 557 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  12. hrbrmstr created this gist Sep 20, 2014.
    43 changes: 43 additions & 0 deletions cantons.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    library(rvest)
    library(dplyr)
    library(httr) # >0.5
    library(tidyr)
    library(gpclib)
    library(rgeos)
    library(sp)
    library(maptools)
    library(rgdal) # needs gdal > 1.11.0
    library(ggplot2)
    library(reshape2)
    library(gridExtra)

    map = readOGR("readme-swiss.json", "cantons")
    map_df <- fortify(map)

    dat <- data.frame(id=0:(length(map@data$name)-1), canton=map@data$name)
    dat$color <- "blue"
    dat[2,]$color <- "red"
    dat

    map_df <- merge(map_df, dat, by="id")

    centers <- data.frame(gCentroid(map, byid=TRUE))
    centers$canton <- dat$canton

    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_bw()
    gg <- gg + theme(panel.grid=element_blank())
    gg <- gg + theme(legend.position="none")
    gg <- gg + theme(panel.border=element_blank())
    gg <- gg + theme(axis.ticks=element_blank())
    gg <- gg + theme(axis.text=element_blank())
    gg

    ggsave("cantons.svg", gg, width=9, height=6)
    557 changes: 557 additions & 0 deletions cantons.svg
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    1 change: 1 addition & 0 deletions readme-swiss.json
    1 addition, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.