Skip to content

Instantly share code, notes, and snippets.

@amandamasonsingh
Forked from cdesante/basicmap.r
Last active August 29, 2015 14:27
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 amandamasonsingh/141574e8ef1ac456a22f to your computer and use it in GitHub Desktop.
Save amandamasonsingh/141574e8ef1ac456a22f to your computer and use it in GitHub Desktop.
basic map
# I had to make a few tweaks to my code to get this to work.
# I also was never able to load the Prison data, so I used my own data.
# To stay consistent with this example (where this code is forked from), I am calling data data "Prison" here.
# Here are the changes that I made.
# For example, instead of:
# Total <- merge(all_states, Prison, by="region")
# I had to do:
# Total <- merge(all_states, Prison, all=TRUE)
# However, after doing that, my map had a bunch of lines running through it.
# To fix it, I found the following to be helpful
# (tweaked from http://stackoverflow.com/questions/23714052/ggplot-mapping-us-counties-problems-with-visualization-shapes-in-r).
# See the updated code below:
#*****************
loading packages
#*****************
#for maps
library(maps)
#help(package="maps") #help files if needed
#for plotting data
library(ggplot2)
#help(package="ggplot2") #help files if needed
#this package loads tables faster than merge
library(data.table)
#help(package="data.table") #if you need help with the package later
#to get fancy colors for graphing
#can see some color palettes here: http://moderndata.plot.ly/wp-content/uploads/2015/04/seq-300x211.jpg
library(RColorBrewer)
#help(package="RColorBrewer") #if you need help with the package later
##******************
#Matching state map data frame to Prison data frame
#********************
#setting keys to match the two data frames (all_states and Prison)
all_states <- data.table(map_data('state'))
setkey(all_states,region)
Prison <- data.table(Prison)
setkey(Prison,stateName)
#now merging the two data frames together based on those keys
map.df <- all_states[Prison]
#*************************
#now creating the state map
#**************************
ggplot(map.df, aes(x=long, y=lat, group=group, fill=bwRatio)) +
scale_fill_gradientn("",colours=brewer.pal(9,"GnBu"))+
geom_polygon()+coord_map()+
labs(fill="Black to White Incarceration Rates \n Weighted by Relative Population",
title="State Incarceration Rates by Race, 2010",x="",y="")+theme_bw()
##################################
##################################
##################################ORIGINAL CODE IS BELOW - DIDN'T WORK FOR ME AS WRITTEN################################
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