Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created December 6, 2024 21:18
Show Gist options
  • Save andrewheiss/3969c956ca7368951ecf57ccc95bbd11 to your computer and use it in GitHub Desktop.
Save andrewheiss/3969c956ca7368951ecf57ccc95bbd11 to your computer and use it in GitHub Desktop.
library(tidyverse)

# Colors too dark
mtcars |> 
  mutate(carb = factor(carb)) |> 
  ggplot(aes(carb, fill = carb)) +
  geom_bar() +
  scale_fill_viridis_d(
    option = "magma", begin = 0.1, end = 0.8
  ) +
  theme_bw()

# Colors lighter, but gridlines are visible
mtcars |> 
  mutate(carb = factor(carb)) |> 
  ggplot(aes(carb, fill = carb)) +
  geom_bar(alpha = 0.8) +
  scale_fill_viridis_d(
    option = "magma", begin = 0.1, end = 0.8
  ) +
  theme_bw()

# Colors are lighter without transparency
mtcars |> 
  mutate(carb = factor(carb)) |> 
  ggplot(aes(x = carb)) +
  geom_bar(
    aes(fill = stage(
      start = carb,
      after_scale = colorspace::lighten(fill, 0.15)
    ))
  ) +
  scale_fill_viridis_d(
    option = "magma", begin = 0.1, end = 0.8
  ) +
  theme_bw()

Created on 2024-12-06 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment