Created
December 10, 2012 17:55
-
-
Save cdesante/4252133 to your computer and use it in GitHub Desktop.
basic map
This file contains hidden or 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
doInstall <- TRUE | |
toInstall <- c("maps", "ggplot2") | |
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
lapply(toInstall, library, character.only = TRUE) | |
library(ggplot2) | |
library(maps) | |
Prison <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/prison.csv") | |
head(Prison) | |
all_states <- map_data("state") | |
all_states | |
head(all_states) | |
Prison$region <- Prison$stateName | |
Total <- merge(all_states, Prison, by="region") | |
head(Total) | |
Total <- Total[Total$region!="district of columbia",] | |
p <- ggplot() | |
p <- p + geom_polygon(data=Total, aes(x=long, y=lat, group = group, fill=Total$bwRatio),colour="white" | |
) + scale_fill_continuous(low = "thistle2", high = "darkred", guide="colorbar") | |
P1 <- p + theme_bw() + labs(fill = "Black to White Incarceration Rates \n Weighted by Relative Population" | |
,title = "State Incarceration Rates by Race, 2010", x="", y="") | |
P1 + scale_y_continuous(breaks=c()) + scale_x_continuous(breaks=c()) + theme(panel.border = element_blank()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It appears you can fix the "splintered map/random lines in the map" issue simply by ordering your variables by the "order" variable in all_states after your last merger.
I.e. just insert Total <- Total[order(Total$order),] before running the plotting functions. (see http://stackoverflow.com/questions/23714052/ggplot-mapping-us-counties-problems-with-visualization-shapes-in-r, last answer)
The main difference using the "all = TRUE" argument in the initial merger (Total <- merge(all_states, Prison, all=TRUE)) seems to be that if you DON'T set "all = TRUE" and you are missing data for single states they will be shown as opaque, whereas if you do set "all = TRUE" the states will be inserted but not colored.
Thanks @amandamasonsingh for the code revisions, pointing to the stackoverflow thread, and highlighting the "all = TRUE" issue!