Last active
February 24, 2016 23:10
-
-
Save cavedave/aaecba3fc0d187995e17 to your computer and use it in GitHub Desktop.
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(statebins) | |
library(dplyr) | |
#get population data from https://www.census.gov/popest/data/national/totals/2015/files/NST-EST2015-popchg2010_2015.csv | |
pop<-read.csv("NST.csv", header = TRUE, sep = ",", quote = "\"",dec = ".", fill = TRUE, comment.char = "") | |
#data wangling to get only states and cast as characters | |
pop15 <- select(pop, NAME, POPESTIMATE2015) | |
pop15s<-slice(pop15, 6:56) | |
pops<-rename(pop15s, c(NAME=state,POPESTIMATE2015=pop)) | |
i <- sapply(pops, is.factor) | |
pops[i] <- lapply(pops[i], as.character) | |
statebins(pops, value_col="pop", text_color="black", font_size=3, | |
legend_title = "US States Population", plot_title="US States Colored by Population",legend_position="bottom",title_position="top") | |
## End(Not run) | |
ggsave(file="Us-Pop.png") | |
############################ | |
#https://www.census.gov/2010census/csv/pop_density.csv | |
popd<-read.csv("pop_density.csv", header = TRUE, sep = ",", quote = "\"",dec = ".", fill = TRUE, comment.char = "") | |
popd15 <- select(popd, STATE_OR_REGION, X2010_DENSITY) | |
pop15s<-slice(pop15, 6:56) | |
popd15 <- filter(popd15, !grepl("United States",STATE_OR_REGION)) | |
popd15 <- filter(popd15, !grepl("Puerto",STATE_OR_REGION)) | |
popd15 <- filter(popd15, !grepl("District of Columbia",STATE_OR_REGION)) | |
popsd<-rename(popd15, c("STATE_OR_REGION"="state","X2010_DENSITY"="pop")) | |
i <- sapply(popsd, is.factor) | |
popsd[i] <- lapply(popsd[i], as.character) | |
statebins(popsd, value_col="pop", text_color="black", font_size=3, | |
legend_title = "US States Population Density", plot_title="US States Colored by Population Density",legend_position="bottom",title_position="top") | |
## End(Not run) | |
ggsave(file="Us-PopD.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment