boB Rudis
4 years ago
14 changed files with 259 additions and 3 deletions
@ -0,0 +1,2 @@ |
|||||
|
YEAR: 2020 |
||||
|
COPYRIGHT HOLDER: Bob Rudis |
@ -0,0 +1,21 @@ |
|||||
|
# MIT License |
||||
|
|
||||
|
Copyright (c) 2020 Bob Rudis |
||||
|
|
||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
of this software and associated documentation files (the "Software"), to deal |
||||
|
in the Software without restriction, including without limitation the rights |
||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
|
copies of the Software, and to permit persons to whom the Software is |
||||
|
furnished to do so, subject to the following conditions: |
||||
|
|
||||
|
The above copyright notice and this permission notice shall be included in all |
||||
|
copies or substantial portions of the Software. |
||||
|
|
||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
|
SOFTWARE. |
@ -1,4 +1,6 @@ |
|||||
# Generated by roxygen2: do not edit by hand |
# Generated by roxygen2: do not edit by hand |
||||
|
|
||||
|
export(gg_catchpole) |
||||
|
export(read_delegates) |
||||
import(httr) |
import(httr) |
||||
importFrom(jsonlite,fromJSON) |
importFrom(jsonlite,fromJSON) |
||||
|
@ -0,0 +1,21 @@ |
|||||
|
#' Retrieves current U.S. State delegate assignments for 2020 from the WSJ |
||||
|
#' |
||||
|
#' @export |
||||
|
#' @references <https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json> |
||||
|
read_delegates <- function() { |
||||
|
|
||||
|
jsonlite::fromJSON( |
||||
|
url("https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json"), |
||||
|
simplifyDataFrame = FALSE |
||||
|
) -> del |
||||
|
|
||||
|
state_del <- del |
||||
|
state_del$data[["US"]] <- NULL |
||||
|
|
||||
|
map_df(state_del$data, ~bind_cols(.x$delCount), .id = "state") %>% |
||||
|
gather(candidate, delegates, -state) %>% |
||||
|
filter(delegates > 0) -> states |
||||
|
|
||||
|
states |
||||
|
|
||||
|
} |
@ -0,0 +1,71 @@ |
|||||
|
#' Plot a U.S. Democratic Delegates Cargogram (2020) |
||||
|
#' |
||||
|
#' @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 |
||||
|
gg_catchpole <- 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 |
||||
|
|
||||
|
}) -> candidates_expanded |
||||
|
|
||||
|
c( |
||||
|
"Biden" = "#5ac4c2", |
||||
|
"Sanders" = "#63bc51", |
||||
|
"Warren" = "#9574ae", |
||||
|
"Buttigieg" = "#007bb1", |
||||
|
"Klobuchar" = "#af973a", |
||||
|
"Bloomberg" = "#AA4671", |
||||
|
"Steyer" = "#4E4EAA", |
||||
|
"Yang" = "#C76C48", |
||||
|
"Gabbard" = "#7B8097" |
||||
|
) -> dcols |
||||
|
|
||||
|
gsf <- left_join(delegates_sf, candidates_expanded, by = c("state", "idx")) |
||||
|
|
||||
|
ggplot() + |
||||
|
geom_sf( |
||||
|
data = gsf, |
||||
|
aes(fill = candidate), |
||||
|
col = "#617a89", shape = 22, size = 2, stroke = 0.125 |
||||
|
) + |
||||
|
scale_fill_manual( |
||||
|
name = NULL, values = dcols, na.value = "gray20", |
||||
|
limits = intersect(unique(delegates$candidate), names(dcols)) |
||||
|
) + |
||||
|
guides( |
||||
|
fill = guide_legend( |
||||
|
override.aes = list(size = 4) |
||||
|
) |
||||
|
) + |
||||
|
coord_sf(datum = NA) |
||||
|
|
||||
|
} |
Binary file not shown.
@ -0,0 +1,80 @@ |
|||||
|
|
||||
|
[![Project Status: Active – The project has reached a stable, usable |
||||
|
state and is being actively |
||||
|
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) |
||||
|
[![Signed |
||||
|
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr) |
||||
|
![Signed commit |
||||
|
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg) |
||||
|
[![Linux build |
||||
|
Status](https://travis-ci.org/hrbrmstr/catchpole.svg?branch=master)](https://travis-ci.org/hrbrmstr/catchpole) |
||||
|
![Minimal R |
||||
|
Version](https://img.shields.io/badge/R%3E%3D-3.2.0-blue.svg) |
||||
|
![License](https://img.shields.io/badge/License-MIT-blue.svg) |
||||
|
|
||||
|
# catchpole |
||||
|
|
||||
|
catchpole title goes here otherwise CRAN checks fail |
||||
|
|
||||
|
## Description |
||||
|
|
||||
|
A good description goes here otherwise CRAN checks fail. |
||||
|
|
||||
|
## What’s Inside The Tin |
||||
|
|
||||
|
The following functions are implemented: |
||||
|
|
||||
|
- `gg_catchpole`: Plot a U.S. Democratic Delegates Cargogram (2020) |
||||
|
- `read_delegates`: Retrieves current U.S. State delegate assignments |
||||
|
for 2020 from the WSJ |
||||
|
|
||||
|
## Installation |
||||
|
|
||||
|
``` r |
||||
|
remotes::install_git("https://git.rud.is/hrbrmstr/catchpole.git") |
||||
|
# or |
||||
|
remotes::install_git("https://git.sr.ht/~hrbrmstr/catchpole") |
||||
|
# or |
||||
|
remotes::install_gitlab("hrbrmstr/catchpole") |
||||
|
# or |
||||
|
remotes::install_bitbucket("hrbrmstr/catchpole") |
||||
|
``` |
||||
|
|
||||
|
NOTE: To use the ‘remotes’ install options you will need to have the |
||||
|
[{remotes} package](https://github.com/r-lib/remotes) installed. |
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
``` r |
||||
|
library(catchpole) |
||||
|
|
||||
|
# current version |
||||
|
packageVersion("catchpole") |
||||
|
## [1] '0.1.0' |
||||
|
``` |
||||
|
|
||||
|
``` r |
||||
|
library(sf) |
||||
|
library(catchpole) # hrbrmstr/catchpole |
||||
|
library(hrbrthemes) |
||||
|
library(tidyverse) |
||||
|
|
||||
|
gg_catchpole() + |
||||
|
theme_ft_rc(grid="") + |
||||
|
theme(legend.position = "bottom") |
||||
|
``` |
||||
|
|
||||
|
<img src="man/figures/README-u-1-1.png" width="1300" /> |
||||
|
|
||||
|
## catchpole Metrics |
||||
|
|
||||
|
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | |
||||
|
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: | |
||||
|
| R | 5 | 0.83 | 72 | 0.83 | 27 | 0.61 | 19 | 0.39 | |
||||
|
| Rmd | 1 | 0.17 | 15 | 0.17 | 17 | 0.39 | 30 | 0.61 | |
||||
|
|
||||
|
## Code of Conduct |
||||
|
|
||||
|
Please note that this project is released with a Contributor Code of |
||||
|
Conduct. By participating in this project you agree to abide by its |
||||
|
terms. |
@ -0,0 +1,12 @@ |
|||||
|
## code to prepare `DATASET` dataset goes here |
||||
|
|
||||
|
library(sf) |
||||
|
library(xml2) |
||||
|
library(hrbrthemes) |
||||
|
library(tidyverse) |
||||
|
|
||||
|
delegates_sf <- st_read("https://rud.is/dl/delegates.geojson", stringsAsFactors = FALSE) |
||||
|
|
||||
|
|
||||
|
|
||||
|
usethis::use_data(delegates_sf, internal = TRUE) |
After Width: | Height: | Size: 69 KiB |
@ -0,0 +1,16 @@ |
|||||
|
% Generated by roxygen2: do not edit by hand |
||||
|
% Please edit documentation in R/gg-catchpole.R |
||||
|
\name{gg_catchpole} |
||||
|
\alias{gg_catchpole} |
||||
|
\title{Plot a U.S. Democratic Delegates Cargogram (2020)} |
||||
|
\usage{ |
||||
|
gg_catchpole(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{ |
||||
|
Plot a U.S. Democratic Delegates Cargogram (2020) |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
% Generated by roxygen2: do not edit by hand |
||||
|
% Please edit documentation in R/get-delegates.R |
||||
|
\name{read_delegates} |
||||
|
\alias{read_delegates} |
||||
|
\title{Retrieves current U.S. State delegate assignments for 2020 from the WSJ} |
||||
|
\usage{ |
||||
|
read_delegates() |
||||
|
} |
||||
|
\description{ |
||||
|
Retrieves current U.S. State delegate assignments for 2020 from the WSJ |
||||
|
} |
||||
|
\references{ |
||||
|
\url{https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json} |
||||
|
} |
Loading…
Reference in new issue