csdaw / ggprism

ggplot2 extension inspired by GraphPad Prism
https://csdaw.github.io/ggprism/
170 stars 21 forks source link

adding new themes #3

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi!

i chanced upon your package and was wondering how i can add new themes to it?

Thank you very much and look forward to hearing back from you.

csdaw commented 3 years ago

Hi there,

I'm happy you stumbled across my package (though its not quite finished/documented yet as you can see...).

The easiest way to add a theme would be to just define your own theme function that builds upon theme_prism(). See the ggplot2 book or this blog post for more details.

Here is how I would do it (let me know if this helps):

library(ggplot2)
library(ggprism)

theme_new <- function(base_size = 14,
                      base_family = "sans",
                      base_fontface = "plain",
                      base_line_size = base_size / 14,
                      base_rect_size = base_size / 14,
                      axis_text_angle = 0,
                      border = FALSE) {
  theme_prism(palette = "black_and_white",
              base_size = base_size,
              base_family = base_family,
              base_fontface = base_fontface,
              base_line_size = base_line_size,
              base_rect_size = base_rect_size,
              axis_text_angle = axis_text_angle,
              border = border) %+replace% 
    theme(panel.background = element_rect(fill = "pink",
                                          colour = NA),
          axis.text = element_text(colour = "red"),
          axis.title = element_text(face = "bold", colour = "orange"))
}

p <- ggplot(mtcars, aes(x = disp, y = mpg)) + 
  geom_point()

p + theme_prism() + labs(title = "theme_prism()")

p + theme_new() + labs(title = "theme_new()")

Created on 2020-12-13 by the reprex package (v0.3.0)