Skip to content

Instantly share code, notes, and snippets.

@johnjosephhorton
Created January 28, 2018 03:52
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 johnjosephhorton/571e81fa81e20ed72d770a62cb4d2dcb to your computer and use it in GitHub Desktop.
Save johnjosephhorton/571e81fa81e20ed72d770a62cb4d2dcb to your computer and use it in GitHub Desktop.
library(geofacet) # my forked version
library(ggplot2)
library(jsonlite)
library(ggrepel)
# grab top cities
cities <- fromJSON("https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json")
n <- 50
x <- cities$longitude[1:n]
y <- cities$latitude[1:n]
index <- as.integer(cities$rank[1:n])
city <- cities$city[1:n]
# illustrate putting on n x n grid
row <- rank(y)
col <- rank(x)
df <- data.frame(x,y,row,col,index, city)
# raw coordinates
ggplot(data = df, aes(x = x,y = y)) +
geom_text_repel(aes(label = city)) +
theme_bw()
# on n x n grid
ggplot(data = df, aes(x = col,y = row)) +
geom_tile(fill="grey", colour = "black") +
geom_text_repel(aes(label = city)) +
theme_bw()
# grid a reduced grid
df <- create_new_grid(x,y,city, num.iterations = 500)
# plus results
ggplot(data = df, aes(x = new.col,y = new.row)) +
geom_point() +
geom_tile(fill="grey", colour = "black") +
geom_text_repel(aes(label = unit.name), size = 2) +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment