boB Rudis
4 years ago
12 changed files with 165 additions and 56 deletions
@ -1,6 +1,23 @@ |
|||
# Generated by roxygen2: do not edit by hand |
|||
|
|||
export(delegates_map) |
|||
export(delegates_pal) |
|||
export(expand_candidates) |
|||
export(gg_catchpole) |
|||
export(read_delegates) |
|||
import(httr) |
|||
import(sf) |
|||
importFrom(dplyr,arrange) |
|||
importFrom(dplyr,bind_cols) |
|||
importFrom(dplyr,filter) |
|||
importFrom(dplyr,left_join) |
|||
importFrom(dplyr,tibble) |
|||
importFrom(ggplot2,aes) |
|||
importFrom(ggplot2,coord_sf) |
|||
importFrom(ggplot2,ggplot) |
|||
importFrom(ggplot2,guide_legend) |
|||
importFrom(ggplot2,guides) |
|||
importFrom(ggplot2,scale_fill_manual) |
|||
importFrom(hrbrthemes,theme_ft_rc) |
|||
importFrom(jsonlite,fromJSON) |
|||
importFrom(purrr,map_df) |
|||
importFrom(tidyr,gather) |
|||
|
@ -1,9 +1,21 @@ |
|||
#' ... |
|||
#' |
|||
#' 2020 Democrat POTUS Delegate Map Tracker |
|||
#' |
|||
#' @section Credit: |
|||
#' |
|||
#' The hard work was done by WSJ's Brian McGill ([@@brian_mcgill](https://twitter.com/brian_mcgill/)) who **manually crafted** the base cartogram over **days** then translated that into the beautiful and illuminating creation that is the WSJ online delegate tracker: <https://www.wsj.com/graphics/elections/2020/track-the-delegate-count/>. |
|||
#' |
|||
#' Brians's tweetnouncement: <https://twitter.com/brian_mcgill/status/1222556373864648704?ref_src=twsrc%5Etfw> |
|||
#' |
|||
#' @md |
|||
#' @name catchpole |
|||
#' @keywords internal |
|||
#' @author Bob Rudis (bob@@rud.is) |
|||
#' @import httr |
|||
#' @author Brian McGill |
|||
#' @import sf |
|||
#' @importFrom purrr map_df |
|||
#' @importFrom dplyr left_join filter bind_cols tibble arrange |
|||
#' @importFrom tidyr gather |
|||
#' @importFrom ggplot2 ggplot aes scale_fill_manual guides guide_legend coord_sf |
|||
#' @importFrom hrbrthemes theme_ft_rc |
|||
#' @importFrom jsonlite fromJSON |
|||
"_PACKAGE" |
|||
|
@ -0,0 +1,6 @@ |
|||
#' Retrieves the delegates basemap |
|||
#' |
|||
#' @export |
|||
delegates_map <- function() { |
|||
delegates_sf |
|||
} |
@ -0,0 +1,15 @@ |
|||
#' WSJ palette for the candidates likely getting delegates |
|||
#' |
|||
#' @docType data |
|||
#' @export |
|||
c( |
|||
"Biden" = "#5ac4c2", |
|||
"Sanders" = "#63bc51", |
|||
"Warren" = "#9574ae", |
|||
"Buttigieg" = "#007bb1", |
|||
"Klobuchar" = "#af973a", |
|||
"Bloomberg" = "#AA4671", |
|||
"Steyer" = "#4E4EAA", |
|||
"Yang" = "#C76C48", |
|||
"Gabbard" = "#7B8097" |
|||
) -> delegates_pal |
@ -0,0 +1,40 @@ |
|||
#' Expand the state totals per delegate into an indexed data frame |
|||
#' |
|||
#' @param delegates a data frame with `state`, `candidate`, and |
|||
#' `delegates` count or use the default which gets the |
|||
#' current data as long as the WSJ keeps it alive. |
|||
#' @export |
|||
expand_candidates <- function(delegates = read_delegates()) { |
|||
|
|||
map_df(unique(delegates$state), ~{ |
|||
|
|||
filter(delegates, state == .x) %>% |
|||
arrange(desc(delegates)) -> cur |
|||
|
|||
tibble( |
|||
state = character(0), |
|||
candidate = character(0), |
|||
idx = integer(0) |
|||
) -> res |
|||
|
|||
last_idx <- 0 |
|||
|
|||
for (cand in unique(cur$candidate)) { |
|||
|
|||
tibble( |
|||
state = cur$state[1], |
|||
candidate = cand, |
|||
idx = (last_idx+1):(last_idx+1+cur[cur$candidate == cand,]$delegates) |
|||
) -> out |
|||
|
|||
last_idx <- tail(out$idx, 1) |
|||
|
|||
res <- bind_rows(res, out) |
|||
|
|||
} |
|||
|
|||
res |
|||
|
|||
}) |
|||
|
|||
} |
@ -0,0 +1,11 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/delegates-map.R |
|||
\name{delegates_map} |
|||
\alias{delegates_map} |
|||
\title{Retrieves the delegates basemap} |
|||
\usage{ |
|||
delegates_map() |
|||
} |
|||
\description{ |
|||
Retrieves the delegates basemap |
|||
} |
@ -0,0 +1,14 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/delegates-pal.R |
|||
\docType{data} |
|||
\name{delegates_pal} |
|||
\alias{delegates_pal} |
|||
\title{WSJ palette for the candidates likely getting delegates} |
|||
\format{An object of class \code{character} of length 9.} |
|||
\usage{ |
|||
delegates_pal |
|||
} |
|||
\description{ |
|||
WSJ palette for the candidates likely getting delegates |
|||
} |
|||
\keyword{datasets} |
@ -0,0 +1,16 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/expand-candidates.R |
|||
\name{expand_candidates} |
|||
\alias{expand_candidates} |
|||
\title{Expand the state totals per delegate into an indexed data frame} |
|||
\usage{ |
|||
expand_candidates(delegates = read_delegates()) |
|||
} |
|||
\arguments{ |
|||
\item{delegates}{a data frame with \code{state}, \code{candidate}, and |
|||
\code{delegates} count or use the default which gets the |
|||
current data as long as the WSJ keeps it alive.} |
|||
} |
|||
\description{ |
|||
Expand the state totals per delegate into an indexed data frame |
|||
} |
Loading…
Reference in new issue