Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom r2dii color scales #28

Closed
cjyetman opened this issue Jan 22, 2021 · 0 comments · Fixed by #399
Closed

add custom r2dii color scales #28

cjyetman opened this issue Jan 22, 2021 · 0 comments · Fixed by #399

Comments

@cjyetman
Copy link
Member

Now that #19 adds a custom ggplot theme, let's also add color scales to be able to easily use the 2dii color palette. Something like...

library(ggplot2)
library(r2dii.ggplot)


r2dii_pal <- function() {
  values <- r2dii_palette_colours()$colour_hex
  max_n <- length(values)
  f <- scales:::manual_pal(values)
  attr(f, "max_n") <- max_n
  f
}

scale_colour_r2dii <- function(...) {
  discrete_scale("colour", "r2dii", r2dii_pal(), ...)
}

scale_color_r2dii <- scale_colour_r2dii

scale_fill_r2dii <- function(...) {
  discrete_scale("fill", "r2dii", r2dii_pal(), ...)
}


scatter <- 
  ggplot(mtcars) +
  geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
  facet_wrap(~am) +
  ggtitle("Diamond Prices")

scatter_r2dii <- scatter + theme_2dii_ggplot() + scale_color_r2dii()

bar <- ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar()
bar_r2dii <- bar + theme_2dii_ggplot() + scale_fill_r2dii()

gridExtra::grid.arrange(scatter, scatter_r2dii, bar, bar_r2dii, nrow = 2)

r2dii_theme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant