knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  fig.width = 5, fig.height = 1
)

tinter

lifecycle R build status Coverage status License: MIT CRAN status CRAN Downloads

Introduction

tinter provides a simple way to generate monochromatic palettes. Easily define:

library(tinter)
hex <- "#335CAC"
tinter_plot <- function(x) {
  grid <- c(length(x), 1)
  width <- 0.9 / (max(grid) + 1)
  gap <- 1 / (max(grid) + 1)
  centres <- lapply(grid, function(i) {
    gap * ((max(grid) -
      i) / 2 + seq_len(i))
  })
  centres <- as.matrix(expand.grid(centres))
  oldPars <- graphics::par(mai = c(0, 0, 0, 0), bg = "white")
  on.exit(graphics::par(oldPars))
  devSize <- grDevices::dev.size()
  devRatio <- devSize[2] / devSize[1]
  graphics::plot(NA, NA,
    xlim = c(-0.1, 1.1), ylim = 0.5 + c(-1, 1) *
      devRatio * 0.6, xlab = "", ylab = "", xaxt = "n", yaxt = "n",
    bty = "n", asp = 1
  )
  graphics::rect(centres[, 1] - width / 2, rev(centres[, 2]) - width / 2,
    centres[, 1] + width / 2, rev(centres[, 2]) + width / 2,
    col = x, border = "white", lwd = 0.2
  )
}

tinter_plot(hex)
tinter(hex)
tinter_plot(tinter(hex))
tinter(hex, direction = "tints")
tinter_plot(tinter(hex, direction = "tints"))
tinter(hex, steps = 10)
tinter_plot(tinter(hex, steps = 10))
tinter(hex, steps = 10, crop = 7)
tinter_plot(tinter(hex, steps = 10, crop = 7))
tinter(hex, steps = 10, crop = 7, adjust = 0.4)
tinter_plot(tinter(hex, steps = 10, crop = 7, adjust = 0.4))

Create a choropleth map

library(ggplot2)
library(sf)

nc <- st_read(system.file(package = "sf", "shape/nc.shp"))

ggplot(data = nc) +
  geom_sf(aes(fill = AREA), colour = "white", lwd = 0.04) +
  # colours from tinter
  scale_fill_gradientn(colours = tinter(hex)) +
  theme_void() +
  coord_sf(datum = NA)

Doesn't this already exist?

tinter just simplifies a task usually done with grDevices. It's default is to remove black and white from the palette.

tinter("blue")

### ------ is identical to

grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1, 11))]
tinter("blue", direction = "shades")

### --- is identical to

grDevices::colorRampPalette(colors = c("blue", "black"))(6)[-6]
tinter("blue", crop = 2)

### --- is identical to

grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1:2, 10:11))]

Installation

To install the latest release from CRAN

install.packages("tinter")

To install the developmental version from GitHub

# install.packages("remotes")
remotes::install_github("poissonconsulting/tinter")

Contribution

Please report any issues.

Pull requests are always welcome.

Please note that the tinter project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



poissonconsulting/tinter documentation built on Aug. 14, 2020, 4:16 p.m.