davidhodge931 / ggblanket

Simplify ggplot2 visualisation
https://davidhodge931.github.io/ggblanket/
Other
156 stars 8 forks source link

`*_palette`: support `col_palette_colour` and `col_palette_fill` #957

Closed davidhodge931 closed 2 months ago

davidhodge931 commented 3 months ago

In gg_*:

In set_blanket, weave_geom_defaults and weave_col_palette_* :

davidhodge931 commented 3 months ago

Maybe add alpha_recursive = NULL and think about the rest for a while

davidhodge931 commented 3 months ago

Doesn't work that well for setting col_palette_colour, as it would affect geoms with no fill

Also, doesn't work that well for lighten as changes the colour of the main thing you see..

Maybe just add alpha_recursive and mull on the rest of it

davidhodge931 commented 3 months ago
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.4.1
library(ggblanket)
library(palmerpenguins)
library(colorspace)

set_blanket(
  alpha_recursive = 1,
)

penguins |> 
  gg_density(
    x = flipper_length_mm,
    colour = darken(blue, 0.33),
  )
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_density()`).


penguins |> 
  gg_density(
    x = flipper_length_mm,
    col = species,
  ) +
  scale_colour_manual(values = darken(jumble, 0.33))
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_density()`).

Created on 2024-07-23 with reprex v2.1.0

davidhodge931 commented 3 months ago

Doesn't work so well as using alpha, as you need different defaults for geoms with only colour, compared to those with colour/fill

davidhodge931 commented 3 months ago
penguins |>
  tidyr::drop_na(sex) |>
  group_by(sex, species) |>
  summarise(across(flipper_length_mm, \(x) mean(x, na.rm = TRUE))) |>
  gg_col(
    x = flipper_length_mm,
    y = species,
    col = sex,
    position = position_dodge(preserve = "single"),
    width = 0.75,
    col_labels = \(x) str_sub(x, 1, 1), # doesn't work
    col_breaks = "female", # doesn't work
  ) +
  scale_colour_manual(values = darken(jumble, 0.33))
davidhodge931 commented 3 months ago

Better solution: col_palette could accept a list of 2 elements for the colour and fill colours

col_palette = list(jumble, lighten(jumble, 0.2))

davidhodge931 commented 3 months ago
aes_darken_colour <- function(amount = 0.1, ...) {
  aes(colour = ggplot2::after_scale(colorspace::darken(fill, amount = amount)))
}

aes_lighten_colour <- function() {
  aes(colour = ggplot2::after_scale(colorspace::darken(fill, 0.2)))
}

penguins |>
  gg_bar(
    y = species,
    col = island,
    mapping = aes_darken_colour(amount = 0.5),
  )
davidhodge931 commented 3 months ago
library(ggblanket)
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.4.1
library(palmerpenguins)
library(colorspace)

set_blanket(
  alpha_recursive = 1,
  colour = teal,
  col_palette_d = c('red', "blue", "grey"),
)

aes_darken_colour <- function(...) {
  ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(fill, ...)))
}

aes_lighten_colour <- function(...) {
  ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(fill, ...)))
}

penguins |>
  gg_bar(
    y = species,
    colour = colorspace::darken(teal),
  )


penguins |>
  gg_bar(
    y = species,
    col = island,
    mapping = aes_darken_colour(),
  )
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.


penguins |>
  gg_bar(
    y = species,
    colour = colorspace::darken(teal, amount = 0.5),
  )


penguins |>
  gg_bar(
    y = species,
    col = island,
    mapping = aes_darken_colour(amount = 0.5),
  )
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.

Created on 2024-07-23 with reprex v2.1.0

davidhodge931 commented 3 months ago

aes_colour_darken <- function(...) { ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(fill, ...))) }

aes_colour_lighten <- function(...) { ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(fill, ...))) }

aes_colour_contrast <- function(...) { ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(fill, ...))) }

davidhodge931 commented 3 months ago
library(ggblanket)
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.4.1
library(palmerpenguins)
library(colorspace)

set_blanket(
  alpha_recursive = 1,
)

aes_colour_darken <- function(...) {
  ggplot2::aes(colour = ggplot2::after_scale(colorspace::darken(.data$fill, ...)))
}

aes_colour_lighten <- function(...) {
  ggplot2::aes(colour = ggplot2::after_scale(colorspace::lighten(.data$fill, ...)))
}

aes_fill_darken <- function(...) {
  ggplot2::aes(fill = ggplot2::after_scale(colorspace::darken(.data$colour, ...)))
}

aes_fill_lighten <- function(...) {
  ggplot2::aes(fill = ggplot2::after_scale(colorspace::lighten(.data$colour, ...)))
}

penguins |>
  gg_bar(
    y = species,
    colour = darken(blue, amount = 0.2),
    width = 0.75,
  )


penguins |>
  gg_bar(
    y = species,
    col = island,
    mapping = aes_colour_darken(amount = 0.2),
    width = 0.75,
  )
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.

Created on 2024-07-23 with reprex v2.1.0