nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
179 stars 18 forks source link

auto color and/or auto gradient? #56

Closed nutterb closed 7 years ago

nutterb commented 8 years ago

check out this vignette in the condformat package, that does some cool work with automatically assigning colors, gradients, etc.

https://cran.r-project.org/web/packages/condformat/vignettes/introduction.html

the pixiedust equivalent is

library(pixiedust) library(RColorBrewer) data(iris)

output <- iris[c(1:5,70:75, 120:125),]

dust(output) %>%
  sprinkle_table(pad = 3) %>%
  sprinkle(
    cols = "Species",
    bg = c(rep("rgb(249,118,110)", sum(output$Species == "setosa")),
           rep("rgb(1,186,56)", sum(output$Species == "versicolor")),
           rep("rgb(98,156,255)", sum(output$Species == "virginica"))),
    recycle = "rows"
  ) %>%
  sprinkle(
    cols = c("Sepal.Length", "Sepal.Width"),
    rows = which(with(output, Sepal.Width > Sepal.Length - 2.25)),
    bg = "#7D00FF"
  ) %>%
  sprinkle(
    cols = "Petal.Length",
    bg = cut(output$Petal.Length, breaks = 9,
             labels = brewer.pal(9, "BrBG")) %>%
           as.character(),
    recycle = "rows"
  ) %>%
  medley_bw()

I think there's potential to do some cool sprinkles that automate some of that work.

nutterb commented 7 years ago

I propose (to myself) two additional sprinkle systems.

For discrete coloring

discrete = c("bg", "font", "border")
discrete_colors = getOption("pixiedust_discrete_pal", NULL)
discrete_lighten_text = "white"

If no colors are given, the default colors from the scales package will be used.

discrete_lighten_text forces text to be white when the background is too dark. Only relevant for background colors. I need to talk to Joel about how he calculated this. Set to NULL to turn it off. May also be any other valid color.

For gradient coloring

gradient = c("bg", "font", "border")
gradient_n = 10 
gradient_colors = getOption("pixied_gradient_color", c("#132B43", "#56B1F7"))
gradient_lighten_text = "white"

gradient_n gives the number of shades to use between first and last colors

gradient_colors gives the colors over which to shade. Must be at least two.

gradient_lighten_text, same as discrete_lighten_text

For reference

library(scales)
library(pixiedust)

pal = gradient_n_pal(c("#132B43", "#56B1F7"))(seq(0, 1, length.out = 10))
pal_font = gradient_n_pal(c("#FFFFFF", "#000000"))(seq(0, 1, length.out = 10))

pal_discrete <- hue_pal()(2)

mpg_cut <- cut(mtcars$mpg,
               breaks = quantile(mtcars$mpg, probs = seq(0, 1, length.out = 10)),
               include.lowest = TRUE)

levels(mpg_cut)

dust(mtcars) %>%
  sprinkle(col = "mpg",
           bg = pal[as.numeric(mpg_cut)],
           font_color = pal_font[as.numeric(mpg_cut)],
           recycle = "rows") %>%
  sprinkle(col = "am",
           bg = pal_discrete[as.numeric(as.factor(mtcars$am))],
           recycle = "rows") %>%
  sprinkle_print_method("html")