Skip to content

Simplify creation of style sets with defaults #90

@clauswilke

Description

@clauswilke

I think the most common use case for style sets in practice is one where we want to use something like classic style but add a few customizations. This is a bit cumbersome with the current functions. See reprex:

library(ggplot2)
library(marquee)

colors <- c(setosa = '#93263E', virginica = '#3947A4', versicolor = '#C6A634')

# set up styling
iris_styles <- classic_style() |>
  modify_style("setosa", style(color = colors["setosa"])) |>
  modify_style("virginica", style(color = colors["virginica"])) |>
  modify_style("versicolor", style(color = colors["versicolor"]))

ggplot(iris) +
  geom_point(aes(Sepal.Length, Sepal.Width, color = Species)) +
  scale_color_manual(
    values = colors,
    guide = guide_marquee("{.setosa *I. setosa*} - {.versicolor *I. versicolor*} - {.virginica *I. virginica*}")
  )

I see at least three options to simplify this, not mutually exclusive.

1. Add a function modify_style_set()

This function would work similarly to modify_style() but enable modification of multiple styles at once. Something like this:

iris_styles <- classic_style() |>
  modify_style_set(
    setosa = style(color = colors["setosa"]),
    virginica = style(color = colors["virginica"]),
    versicolor = style(color = colors["versicolor"])
  )

2. Create style sets with default

This would modify the style_set() function to enable a default option.

iris_styles <- style_set(
  setosa = style(color = colors["setosa"]),
  virginica = style(color = colors["virginica"]),
  versicolor = style(color = colors["versicolor"]),
  .defaults = classic_style()
)

3. Provide an option to update/merge style sets

A function update_style_set() could work similarly to ggplot2:::defaults(), but with the order of arguments reversed.

custom_styles <- style_set(
  setosa = style(color = colors["setosa"]),
  virginica = style(color = colors["virginica"]),
  versicolor = style(color = colors["versicolor"]),
)

iris_styles <- update_style_set(classic_style(), custom_styles)

I'm happy to put in some work to implement this. My suggestion is to start with option 3 and then use it to implement option 1. I'm less sure about option 2 and would leave that aside for now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions