Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active November 20, 2019 15:04
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 cavedave/f50a86e6bf4e7461245f879f36cb9ca0 to your computer and use it in GitHub Desktop.
Save cavedave/f50a86e6bf4e7461245f879f36cb9ca0 to your computer and use it in GitHub Desktop.
#load the libraries
library(ggplot2)
library(grid)
library(dplyr)
#load the data
#https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/export/
zips <-
read.csv("us-zip-code-latitude-and-longitude.csv",
header = TRUE, stringsAsFactors = TRUE,sep=";")
#add in id row
zips$ID <- seq.int(nrow(zips))
#just take data we want for now. Theres cool stuff in this dataset though
zips<-zips%>%
select(ID, Latitude, Longitude)
#filter out very far south places that do not look right
zips<-zips %>%
filter(Latitude>10)
#copy method in this Australian public toilet tutorial
#https://unconj.ca/blog/not-all-population-maps-are-boring.html
#make a map
p <-
ggplot(zips, aes(x = Longitude, y = Latitude)) +
stat_binhex(
colour = NA,
aes(fill = cut(..count.., c(0, 5, 10, 50, 100,
500, 1000, Inf)))
) +
coord_equal() +
labs(fill = NULL) +
scale_fill_brewer(
palette = "OrRd",
labels = c("<5 ", "5-9 ", "10-49 ", "50-99 ",
"100-499 ", "500-999 ", "1000+ ")
)
print(p)
#clean it up a bit
require(grid, quiet = TRUE) # For the unit() function.
p <-p + labs(fill = "Number zipcodes")
p <- p +
#geom_text(aes(x = long, y = lat, label = name, vjust = vjust,
# hjust = hjust), data = poi, size = 4) +
theme(panel.background = element_rect(fill = "gray90", colour = NA),
plot.background = element_rect(fill = "gray90", colour = NA),
# Remove titles, ticks, gridlines, and borders.
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
# Set the legend background.
legend.background = element_rect(fill = NA, colour = NA),
legend.key = element_rect(fill = NA, colour = NA),
)
print(p)
#save image
p<-p+ggtitle("Zip code Density in the US")+theme(plot.title = element_text(hjust = 0.5,size=20, face="bold"))
ggsave("zipcodes.png", height=5, width=10)
@cavedave
Copy link
Author

zipcodes

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