By Julia Silge
Well, it’s been an interesting election season so far, right? Everybody holding up OK? Utah held its caucuses this past Tuesday on March 22 and I thought I would do a bit of plotting to show the results. We can get the JSON data from CNN, as pointed out by Bob Rudis in his post here. Utah’s results were not available when he wrote that post but I was able to poke around and find them using the guidance he provided there. Many thanks to CNN for all the hard work they do in reporting!
library(jsonlite)
utahRJSON fromJSON("http://data.cnn.com/ELECTION/2016primary/UT/county/S.json",
flatten=TRUE)
utahDJSON fromJSON("http://data.cnn.com/ELECTION/2016primary/UT/county/E.json",
flatten=TRUE)
Every county in Utah was won by the same Republican or Democratic candidate, so it will not be particularly interesting to make maps showing who won the caucuses. Instead, what I’m going for here is to see if there are any differences in voting patterns for the various candidates. To do that, let’s make a data frame for each candidate with his/her results. First up, the Republicans.
library(purrr)
library(dplyr)
cruz mutate(map_df(utahRJSON$counties$race.candidates, function(x) {
x %>% filter(lname == "Cruz")
}), FIPS=utahRJSON$counties$countycode)
kasich mutate(map_df(utahRJSON$counties$race.candidates, function(x) {
x %>% filter(lname == "Kasich")
}), FIPS=utahRJSON$counties$countycode)
trump mutate(map_df(utahRJSON$counties$race.candidates, function(x) {
x %>% filter(lname == "Trump")
}), FIPS=utahRJSON$counties$countycode)
This was my first time to use purrr
, Hadley Wickham’s library which is sort of similar to dplyr
but for functions. I am a HUGE fan of …read more
Source:: r-bloggers.com