Skip to content

Instantly share code, notes, and snippets.

@jimjam-slam
Created May 26, 2021 05:44
Show Gist options
  • Save jimjam-slam/1e2210db7bb68a4ec049fecf0154e2d6 to your computer and use it in GitHub Desktop.
Save jimjam-slam/1e2210db7bb68a4ec049fecf0154e2d6 to your computer and use it in GitHub Desktop.
Ggplot2 gradient tests
library(tidyverse)
my_gradient <- grid::linearGradient(
colours = c("#020024", "#102b81", "#833ab4"),
stops = c(0, 0.4, 1))
# works!
myplot <-
ggplot(mtcars) +
aes(mpg, hp, size = disp) +
geom_point() +
theme_grey(base_size = 20) +
theme(
legend.background = element_rect(
fill = my_gradient)) +
labs(title = "Hello gradients!", subtitle = "Another go")
ggsave('test_grad_legend.png', myplot, device = png(type = "cairo-png"))
# nope
myplot <-
ggplot(mtcars %>% rownames_to_column()) +
aes(x = rowname, y = mpg) +
geom_col(fill = my_gradient) +
theme_grey(base_size = 20) +
labs(title = "Hello gradients!", subtitle = "Another go")
ggsave('test_grad_bars.png', myplot, device = png(type = "cairo-png"))
# also nope: passing a list of gradient objects for each geom element
myplot <-
ggplot(mtcars %>% rownames_to_column()) +
aes(x = rowname, y = mpg) +
geom_col(fill = map(1:nrow(mtcars), ~ my_gradient)) +
theme_grey(base_size = 20) +
labs(title = "Hello gradients!", subtitle = "Another go")
ggsave('test_grad_bars.png', myplot, device = png(type = "cairo-png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment