Friday, April 3, 2015

Same Sex Marriages 2015

samesexmarriages
The growing controvery over same sex marriages in USA encouraged many different articles being published by New York Times. One of the articles that caught my attention was Gay Marriages State by State
These types of maps are clasified as Cartograms. The main idea behind the cartograms is preserving the area. In the past I have posted distorted maps which are also a type of cartograms.
New York Times implements the grid sort of maps in many of its visualization. But how do we do it in R? Well “there is a package for it” called statebins. I have implemented the same using the data from Freedom to Marry
. The data i used for this project is available for download here
suppressWarnings(suppressMessages(library(statebins)))
setwd("C:/Users/agohil/Book")
options(stringsAsFactors= FALSE)
data= read.csv("freetomarry.csv")
If you have used R packages in the past you are familiar with the code mentioned above. For beginners please refer to any basic R tutorial or just type ?install.packages(), ?library(), ?options() and ?read.csv.
The SupressWarnings() and SuppressMessages() functions will suppress loading any warning messages. Usually we can ignore warning messages as these will not reallly impact our work.
head(data,2)
##     state value
## 1 Alabama     2
## 2  Alaska     1
class(data$state)
## [1] "character"
class(data$value)
## [1] "integer"
One quick note about the statebins package is that it needs the state names to be of class character and not factor. When you load the data R with read.csv() the function will automatically classify the state column as Factor. To avoid the error we can set the option of stringAsFactos= FALSE within the options() function. Now we are all set to plot the visualization.
The code below is very easy to follow. statebins() function is used to plot the grid like map. the first argument in the function is our dataset, the second argument is state_col which is assigned the column containing the name of every state. Note thate we can also use state abbreviations in place of complete names.
The third argument is the value corresponding to every state. I have assigned 3 different values as statebins requires data to be in a numeric format. We could assign any 3 values in place of 0,1 and 2.
all other arguments are self explanatory. USers can read the manual to understand their use. One of the arguments that does require attention is the brewer_pal which is used to color the grids. The statebins package uses the RColorBrewer package and hence the color format “RdYlGn” may look familiar. If you are new to R you can visit the RColorBrewer or download the package manual to learn more about various options.
We can also specify custom colors as well but it requires a new post.
statebins(data,state_col = "state",value_col = "value",text_color="black", font_size=3,legend_title = "State laws", legend_position="bottom", labels = c("Banned","Allowed","Disputed"),breaks = 3,brewer_pal="RdYlGn", plot_title = "Same-sex Marriages in 2015", title_position = "top")