🍑 Pomological plot theme for ggplot2
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

69 lines
2.6KB

  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. gg_fig <- magick::image_graph(width, height, bg = "transparent", pointsize = pointsize, ...)
  37. print(pomo_gg)
  38. dev.off()
  39. if (!is.null(pomological_overlay) && file.exists(pomological_overlay)) {
  40. pomo_over <- magick::image_read(pomological_overlay)
  41. pomo_over <- magick::image_resize(pomo_over, paste0(width, "x", height, "!"))
  42. gg_fig <- magick::image_composite(gg_fig, pomo_over, "blend", compose_args = "15")
  43. }
  44. # Paint background
  45. if (file.exists(pomological_background)) {
  46. pomo_bg <- magick::image_read(pomological_background)
  47. pomo_bg <- magick::image_resize(pomo_bg, paste0(width, "x", height, "!"))
  48. pomo_bg <- magick::image_crop(pomo_bg, paste0(width, "x", height))
  49. # Paint figure onto background
  50. pomo_img <- magick::image_composite(pomo_bg, gg_fig)
  51. } else pomo_img <- gg_fig
  52. if (!is.null(outfile)) {
  53. # Do you want your picture framed?
  54. magick::image_write(pomo_img, outfile)
  55. }
  56. pomo_img
  57. }
  58. pomological_images <- function(which = c("background", "overlay")) {
  59. which <- match.arg(which)
  60. exts <- c("background" = ".png", "overlay" = ".jpg")
  61. system.file("images", paste0("pomological_", which, exts[which]),
  62. package = "ggpomological")
  63. }