Create Chicklet (Rounded Segmented Column) Charts https://cinc.rud.is/web/packages/ggchicklet/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.4 KiB

---
output: rmarkdown::github_document
editor_options:
5 years ago
chunk_output_type: console
---
```{r pkg-knitr-opts, include=FALSE}
5 years ago
knitr::opts_chunk$set(
collapse=TRUE, fig.retina=2, message=FALSE, warning=FALSE
)
options(width=120)
```
[![Travis-CI Build Status](https://travis-ci.org/hrbrmstr/ggchicklet.svg?branch=master)](https://travis-ci.org/hrbrmstr/ggchicklet)
[![Coverage Status](https://codecov.io/gh/hrbrmstr/ggchicklet/branch/master/graph/badge.svg)](https://codecov.io/gh/hrbrmstr/ggchicklet)
5 years ago
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggchicklet)](https://cran.r-project.org/package=ggchicklet)
# ggchicklet
5 years ago
Create Chicklet (Rounded Segmented Column) Charts
## Description
5 years ago
Sometimes it is useful to stylize column charts a bit more
than just bland rectangles. Methods are provided to create rounded
rectangle segmented column charts (i.e. "chicklets").
## What's Inside The Tin
The following functions are implemented:
## Installation
```{r install-ex, eval=FALSE}
devtools::install_git("https://sr.ht.com/~hrbrmstr/ggchicklet.git")
# or
devtools::install_git("https://gitlab.com/hrbrmstr/ggchicklet.git")
# or (if you must)
devtools::install_github("hrbrmstr/ggchicklet")
```
## Usage
```{r lib-ex}
library(ggchicklet)
# current version
packageVersion("ggchicklet")
```
5 years ago
### From the NYTimes
```{r fig.width=10, fig.height=10, out.width="100%"}
library(stringi)
library(hrbrthemes)
library(tidyverse)
5 years ago
(debates_df <- read_csv("https://rud.is/data/2019-dem-debates.csv.gz"))
5 years ago
5 years ago
debates_df %>%
5 years ago
{ .ordr <<- count(., speaker, wt=elapsed, sort=TRUE) ; . } %>% # order by who had the most time
mutate(speaker = factor(speaker, levels = rev(.ordr$speaker))) %>%
5 years ago
ggplot() +
geom_chicklet(
5 years ago
aes(speaker, elapsed, group = timestamp, fill = topic), # group lets us use temporal order vs fill order
position = position_stack(reverse=TRUE), # reverse otherwise earliest is at end
5 years ago
radius = unit(3, "pt"),
5 years ago
width = 0.6,
color = "white"
5 years ago
) +
coord_flip() +
ggthemes::scale_fill_tableau("Tableau 20") +
scale_x_discrete(expand = c(0, 0.5)) +
scale_y_continuous(
expand = c(0, 0.0625),
position = "right",
breaks = seq(0, 14, 2),
labels = c(0, sprintf("%d min.", seq(2, 14, 2)))
) +
labs(
x = NULL, y = NULL, fill = NULL,
title = "How Long Each Candidate Spoke",
subtitle = "Nights 1 & 2 of the June 2019 Democratic Debates",
caption = "Originals <https://www.nytimes.com/interactive/2019/admin/100000006581096.embedded.html?>\n<https://www.nytimes.com/interactive/2019/admin/100000006584572.embedded.html?>\nby @nytimes Weiyi Cai, Jason Kao, Jasmine C. Lee, Alicia Parlapiano and Jugal K. Patel\nEach bar segment represents the length of a candidate’s response to a question.\n#rstats reproduction by @hrbrmstr"
) +
theme_ipsum_rc(grid="") +
theme(axis.text.x = element_text(color = "gray60", size = 9)) +
theme(axis.ticks = element_line(color = "gray60", size = 0.15)) +
theme(axis.ticks.x = element_line(color = "gray0", size = 0.15)) +
theme(axis.ticks.length = grid::unit(5, "pt")) +
theme(axis.ticks.length.x = grid::unit(5, "pt")) +
theme(legend.position = "bottom")
```
## ggchicklet Metrics
```{r cloc, echo=FALSE}
cloc::cloc_pkg_md()
```
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.