🍑 Pomological plot theme for ggplot2
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

70 lines
2.7KB

  1. #' Paint a ggpomological watercolor
  2. #'
  3. #' Uses [magick] to paint a pomological watercolor. (Paints your plot onto a
  4. #' pomological watercolor style paper, with texture overlay.)
  5. #'
  6. #' @references https://usdawatercolors.nal.usda.gov/pom
  7. #' @seealso [theme_pomological]
  8. #' @param pomo_gg A pomologically styled ggplot2 object. See [theme_pomological()]
  9. #' @param width Width of output image in pixels
  10. #' @param height Height of output image in pixels
  11. #' @param pointsize Text size for plot text
  12. #' @param outfile Optional name for output file if you'd like to save the image
  13. #' @param pomological_background Paper image, defaults to paper texture provided
  14. #' by ggpomological.
  15. #' @param pomological_overlay Overlay texture. Set to `NULL` for no texture.
  16. #' @inheritDotParams magick::image_graph res clip antialias
  17. #' @export
  18. paint_pomological <- function(
  19. pomo_gg,
  20. width = 800,
  21. height = 500,
  22. pointsize = 16,
  23. outfile = NULL,
  24. pomological_background = pomological_images("background"),
  25. pomological_overlay = pomological_images("overlay"),
  26. ...
  27. ) {
  28. if (!requireNamespace("magick", quietly = TRUE)) {
  29. stop("The package magick is required for `paint_pomological()`. ",
  30. "Please install with `install.packages('magick')`")
  31. }
  32. if (!file.exists(pomological_background)) {
  33. warning(paste0("Cannot find file \"", pomological_background, "\""), call. = FALSE)
  34. }
  35. # Paint figure
  36. pomo_gg <- pomo_gg + ggplot2::theme(plot.background = ggplot2::element_rect(fill = 'transparent', colour = NA))
  37. gg_fig <- magick::image_graph(width, height, bg = "transparent", pointsize = pointsize, ...)
  38. print(pomo_gg)
  39. dev.off()
  40. if (!is.null(pomological_overlay) && file.exists(pomological_overlay)) {
  41. pomo_over <- magick::image_read(pomological_overlay)
  42. pomo_over <- magick::image_resize(pomo_over, paste0(width, "x", height, "!"))
  43. gg_fig <- magick::image_composite(gg_fig, pomo_over, "blend", compose_args = "15")
  44. }
  45. # Paint background
  46. if (file.exists(pomological_background)) {
  47. pomo_bg <- magick::image_read(pomological_background)
  48. pomo_bg <- magick::image_resize(pomo_bg, paste0(width, "x", height, "!"))
  49. pomo_bg <- magick::image_crop(pomo_bg, paste0(width, "x", height))
  50. # Paint figure onto background
  51. pomo_img <- magick::image_composite(pomo_bg, gg_fig)
  52. } else pomo_img <- gg_fig
  53. if (!is.null(outfile)) {
  54. # Do you want your picture framed?
  55. magick::image_write(pomo_img, outfile)
  56. }
  57. pomo_img
  58. }
  59. pomological_images <- function(which = c("background", "overlay")) {
  60. which <- match.arg(which)
  61. exts <- c("background" = ".png", "overlay" = ".jpg")
  62. system.file("images", paste0("pomological_", which, exts[which]),
  63. package = "ggpomological")
  64. }