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

https://github.com/pachadotdev/tintin

Palettes generated from Tintin covers. There is one palette per cover, with a total of 24 palettes of 5 colours each. Includes functions to interpolate colors in order to create more colors based on the provided palettes.
https://github.com/pachadotdev/tintin

ggplot2 ggplot2-scales r

Last synced: over 1 year ago
JSON representation

Palettes generated from Tintin covers. There is one palette per cover, with a total of 24 palettes of 5 colours each. Includes functions to interpolate colors in order to create more colors based on the provided palettes.

Awesome Lists containing this project

README

          

---
output: github_document
---

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

# tintin

[![CRAN status](https://www.r-pkg.org/badges/version/tintin)](https://CRAN.R-project.org/package=tintin)

The goal of tintin is to provide palettes generated from Tintin covers. There is one palette per cover, with a total of 24 palettes of 5 colours each. Includes functions to interpolate colors in order to create more colors based on the provided palettes.

```{r pallettes, echo=FALSE, fig.width=7, fig.height=7}
showcol <- function (pal = rev(tintin::tintin_colours), n = NULL, type = "all", exact.n = TRUE) {
gaplist <- ""

maxnum <- 5

palattr <- switch(type, qual = "qualitative", div = "divergent",
seq = "sequential", all = "qualitative+divergent+sequential")
if (is.null(n))
n <- maxnum

if (length(n) == 1)
n <- rep(n, length(pal))

n[n < 3] <- 3
n[n > maxnum] <- maxnum[n > maxnum]
nr <- length(pal)
nc <- max(n)
ylim <- c(0, nr)
oldpar <- par(mgp = c(2, 0.25, 0))
on.exit(par(oldpar))
plot(1, 1, xlim = c(-4, nc), ylim = ylim, type = "n", axes = FALSE,
bty = "n", xlab = "", ylab = "")
for (i in 1:nr) {
nj <- n[i]
if (pal[i] == "")
next
shadi <- pal[[i]]
rect(xleft = 0:(nj - 1), ybottom = i - 1, xright = 1:nj,
ytop = i - 0.2, col = shadi, border = "light grey")
}
text(rep(-0.1, nr), (1:nr) - 0.6, labels = names(pal), xpd = TRUE,
adj = 1)
}

showcol()
```

## Installation

You can install the released version of tintin from [CRAN](https://CRAN.R-project.org) with:

``` r
install.packages("tintin")
```

You can install the development version of tintin like so:

``` r
remotes::install_github("pachadotdev/tintin")
```

## Example

This is a basic example which shows you how to create a plot. We'll plot
the top five causes of injury in the `tintin_head_trauma` dataset that comes
with the package.

```{r example1}
library(dplyr)
library(ggplot2)
library(tintin)

total_head_trauma_5 <- tintin_head_trauma %>%
arrange(-loss_of_consciousness_length) %>%
filter(row_number() <= 5)

ggplot(total_head_trauma_5) +
geom_col(aes(x = cause_of_injury, y = loss_of_consciousness_length,
fill = book_title), position = "dodge") +
labs(x = "Cause of injury", y = "Loss of consciousness length",
title = "Top five causes of injury") +
theme_minimal() +
scale_fill_manual(values = tintin_colours$the_black_island,
name = "Book") +
coord_flip()
```

What is special about the package is being able to pass the colours as a
function to `ggplot2`. We'll adapt the previous example to show that case.

```{r example2}
ggplot(total_head_trauma_5) +
geom_col(aes(x = cause_of_injury, y = loss_of_consciousness_length,
fill = book_title), position = "dodge") +
labs(x = "Cause of injury", y = "Loss of consciousness length",
title = "Top five causes of injury") +
theme_minimal() +
scale_fill_tintin_d(option = "cigars_of_the_pharaoh", direction = -1) +
coord_flip()
```

```{r example2_2, eval = FALSE}
# Note that I can also write the palette name as
# "cigars of the pharaoh" or even as "CiGaRS of ThE Pharaoh"
ggplot(total_head_trauma_5) +
geom_col(aes(x = cause_of_injury, y = loss_of_consciousness_length,
fill = book_title), position = "dodge") +
labs(x = "Cause of injury", y = "Loss of consciousness length",
title = "Top five causes of injury, again") +
theme_minimal() +
scale_fill_tintin_d(option = "cigars of the pharaoh", direction = -1) +
coord_flip()
```

What happens if we need more colours than 5? The functions in the package
can fix that. We'll plot the top ten causes of injury.

```{r example3}
total_head_trauma_10 <- tintin_head_trauma %>%
arrange(-loss_of_consciousness_length) %>%
filter(row_number() <= 10)

ggplot(total_head_trauma_10) +
geom_col(aes(x = cause_of_injury, y = loss_of_consciousness_length,
fill = book_title), position = "dodge") +
labs(x = "Cause of injury", y = "Loss of consciousness length",
title = "Top ten causes of injury") +
scale_fill_manual(values = tintin_clrs(
n = length(unique(total_head_trauma_10$book_title)),
option = "the black island"),
name = "Book") +
coord_flip()
```

```{r example3_2, eval = FALSE}
# or alternatively

ggplot(total_head_trauma_10) +
geom_col(aes(x = cause_of_injury, y = loss_of_consciousness_length,
fill = book_title), position = "dodge") +
labs(x = "Cause of injury", y = "Loss of consciousness length",
title = "Top ten causes of injury") +
scale_fill_manual(values = tintin_pal(option = "the black island")(8),
name = "Book") +
coord_flip()
```

The use of colour instead of fill is analogous. Let's plot the top ten causes of
injury per year to see it.

```{r example4}
library(tidyr)

total_head_trauma_y <- tintin_head_trauma %>%
group_by(year) %>%
summarise_if(is.integer, sum) %>%
pivot_longer(loss_of_consciousness_length:loss_of_consciousness_severity) %>%
mutate(
name = ifelse(name == "loss_of_consciousness_length", "Length", "Severity")
)

ggplot(total_head_trauma_y) +
geom_line(aes(x = year, y = value, color = name), linewidth = 1.5) +
labs(x = "Year", y = "Intensity",
title = "Loss of consciousness length and severity") +
theme_minimal() +
scale_colour_manual(
values = tintin_pal(option = "tintin in the land of the soviets")(2),
name = "Cause of injury"
)
```