An open API service indexing awesome lists of open source software.

https://github.com/ericmshearer/jarvis

dev package
https://github.com/ericmshearer/jarvis

data-science dev r

Last synced: over 1 year ago
JSON representation

dev package

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

library(OCepi)
library(dplyr)
library(jarvis)
library(ggplot2)
```

# jarvis

![](https://img.shields.io/badge/lifecycle-experimental-brightgreen.svg)

## Installation

```r
pak::pak("ericmshearer/jarvis")
```

## Tables

`tbl()`

- `scaled` controls whether or not percent is multiplied by 100.
- `digits` sets rounding.
- For tables >1 variable, can pivot long to wide by setting `pivot` to TRUE.

```{r tbl1}
linelist |>
tbl(Gender)
```

```{r tbl2}
linelist |>
tbl(Gender, Ethnicity, pivot = TRUE)
```

`tbl_totals()`

- Default calculates column totals. For row, set `loc` to "row". Or can use "both".
- Name of summed cell defaults to "Total". To change, update `name`.

```{r tbl_totals}
linelist |>
tbl(Race) |>
tbl_totals()
```

`tbl_percentage()`

- Control rounding via `digits`.
- Default calculates column percentages. For row, set `loc` to "row".

```{r tbl_percentage}
linelist |>
tbl(Race, Ethnicity, pivot = TRUE) |>
tbl_percentage()
```

## Data Visualization

`scale_percent()`

For unscaled percentages (i.e. not multiplied by 100), update `scale` to FALSE.

```{r scale_percent, fig.width = 14, fig.asp = 0.8}
linelist |>
count(Race) |>
mutate(percent = add_percent(n)) |>
ggplot(aes(x = Race, y = percent)) +
geom_bar(stat = "identity") +
scale_x_discrete(label = wrap_labels(delim = "or")) +
scale_y_continuous(expand = c(0,0), label = scale_percent()) +
theme_apollo()
```

`scale_color_apollo()`

- Current palettes: Bright, Muted, Okabe Ito.
- Reverse palette via `reverse`, logical.

```{r scale_color_apollo, fig.width = 16, fig.asp = 0.6}
covid <- read.csv("https://data.chhs.ca.gov/dataset/f333528b-4d38-4814-bebb-12db1f10f535/resource/046cdd2b-31e5-4d34-9ed3-b48cdbc4be7a/download/covid19cases_test.csv", na.strings = "", stringsAsFactors = FALSE) |>
filter(area %in% c("Orange","Los Angeles","San Diego","Ventura","San Bernardino","Kern"))

covid <- covid |>
group_by(area) |>
mutate(
date = as.Date(date, "%Y-%m-%d"),
rate = rate_per_100k(cases, population, digits = 1),
rate_ma_7 = round(zoo::rollmean(rate, k = 7, align = "right", na.pad = FALSE, fill = 0), digits = 2)
) |>
ungroup() |>
filter(date <= "2020-12-23", date > "2020-08-01")

ggplot(data = covid, aes(x = date, y = rate_ma_7, color = area)) +
geom_line(linewidth = 1.2) +
theme_apollo() +
scale_y_continuous(expand = c(0,0), limits = c(0,200)) +
scale_color_apollo(name = "Bright")
```

`expand_x()`

For time series plots that need x-scale extend to fit everything. Set `expand` equal to `expand_x()` with a numeric value.

```{r expand_x, fig.width = 16, fig.asp = 0.6}
ggplot(data = covid, aes(x = date, y = rate_ma_7, color = area)) +
geom_line(linewidth = 1.2) +
geom_text(data = end_points(covid, date), aes(label = area), size = 4.5, hjust = -0.1, show.legend = FALSE) +
theme_apollo() +
scale_x_date(expand = expand_x(20)) +
scale_y_continuous(expand = c(0,0), limits = c(0,200)) +
scale_color_apollo(name = "Bright")
```