🍑 Pomological plot theme for ggplot2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 line
5.9KB

  1. #' Pomological Theme
  2. #'
  3. #' [ggplot2] plot theme based on the USDA Pomological Watercolors paintings.
  4. #'
  5. #' @references https://usdawatercolors.nal.usda.gov/pom
  6. #' @seealso [ggplot2::theme]
  7. #' @param base_family Base text family. See **Fonts** in [theme_pomological()]
  8. #' for some examples from Google Fonts options, including `"Mr De Haviland"`,
  9. #' `"Homemade Apple"`, `"Marck Script"`, and `"Mr. Bedfort"`. For the
  10. #' authentic pomological look, use `"Homemade Apple"` or `"Mr De Haviland"`.
  11. #' Set to `NULL` or use [theme_pomological_plain()] for no change to fonts.
  12. #' @param base_size Base text size
  13. #' @param text.color Color of all text (except axis text, see `axis.text.color`)
  14. #' @param plot.background.color Color of plot background, passed to `plot.background`
  15. #' @param panel.border.color Color of plot panel border
  16. #' @param with.panel.grid If `FALSE` gridlines in plot are removed
  17. #' @param panel.grid.color,panel.grid.linetype Color and linetype of panel grid, passed to `panel.grid`
  18. #' @param axis.text.color,axis.text.size Color and size of axis text
  19. #' @param base_theme Starting theme of plot, default is
  20. #' [ggplot2::theme_minimal()]. Any elements set by `theme_pomological()` will
  21. #' overwrite the `base_theme` unless the specific parameter is explicitly set
  22. #' to `NULL`.
  23. #'
  24. #' @section Fonts:
  25. #' Complete the pomological watercolor theme with a handwriting or cursive font.
  26. #' The following fonts from [Google Fonts](https://fonts.google.com) work well.
  27. #' Visit the links below to install on your system.
  28. #'
  29. #' - [Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)
  30. #' - [Mr. De Haviland](https://fonts.google.com/specimen/Mr+De+Haviland)
  31. #' - [Marck Script](https://fonts.google.com/specimen/Marck+Script/)
  32. #' - [Mr. Bedfort](https://fonts.google.com/specimen/Mr+Bedfort/)
  33. #'
  34. #' Fonts with R are notoriously tricky, so these may not work well for you. If
  35. #' you have installed the fonts but they aren't showing up or working, you can
  36. #' always try running `extrafont::font_import()` or `extrafont::load_fonts()` in
  37. #' the session or RMarkdown document. Or you can use [theme_pomological_plain()].
  38. #'
  39. #' @examples
  40. #' library(ggplot2)
  41. #' basic_iris_plot <- ggplot(iris) +
  42. #' aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
  43. #' geom_point(size = 2) +
  44. #' # with pomological color scale
  45. #' scale_color_pomological()
  46. #'
  47. #' # Pomological Theme
  48. #' basic_iris_plot +
  49. #' theme_pomological()
  50. #'
  51. #' # Don't change panel grid color
  52. #' basic_iris_plot +
  53. #' theme_pomological(
  54. #' panel.grid.color = NULL
  55. #' )
  56. #'
  57. #' # White background
  58. #' basic_iris_plot +
  59. #' theme_pomological_nobg()
  60. #'
  61. #' # Plain plot without font or background
  62. #' basic_iris_plot +
  63. #' theme_pomological_plain()
  64. #'
  65. #' @export
  66. theme_pomological <- function(
  67. base_family = "Homemade Apple",
  68. base_size = 14,
  69. text.color = pomological_base$dark_blue,
  70. plot.background.color = pomological_base$paper,
  71. panel.border.color = pomological_base$light_line,
  72. with.panel.grid = FALSE,
  73. panel.grid.color = pomological_base$light_line,
  74. panel.grid.linetype = "dashed",
  75. axis.text.color = pomological_base$medium_line,
  76. axis.text.size = base_size * 3/4,
  77. base_theme = ggplot2::theme_minimal()
  78. ) {
  79. if (!is.null(base_family)) check_font(base_family)
  80. base_theme +
  81. ggplot2::theme(
  82. text = ggplot2::element_text(
  83. family = base_family,
  84. size = base_size,
  85. colour = text.color
  86. ),
  87. plot.background = ggplot2::element_rect(
  88. fill = plot.background.color,
  89. colour = NA
  90. ),
  91. panel.grid = ggplot2::element_line(
  92. colour = panel.grid.color,
  93. linetype = panel.grid.linetype),
  94. panel.border = ggplot2::element_rect(
  95. color = panel.border.color,
  96. fill = NA,
  97. linetype = "solid",
  98. size = 0.75
  99. ),
  100. panel.grid.major = if (!with.panel.grid) ggplot2::element_blank(),
  101. panel.grid.minor = ggplot2::element_blank(),
  102. axis.text = ggplot2::element_text(
  103. colour = axis.text.color,
  104. size = axis.text.size)
  105. )
  106. }
  107. #' @describeIn theme_pomological Pomological theme with white (transparent) background
  108. #' @export
  109. theme_pomological_nobg <- function(...) {
  110. dots <- list(...)
  111. dots$plot.background.color <- "transparent"
  112. do.call("theme_pomological", args = dots)
  113. }
  114. #' @describeIn theme_pomological A "plain" pomological theme with white
  115. #' background and normal fonts.
  116. #' @export
  117. theme_pomological_plain <- function(...) {
  118. dots <- list(...)
  119. dots$plot.background.color <- "transparent"
  120. if (!"base_family" %in% names(dots)) dots["base_family"] <- ""
  121. if (!"base_size" %in% names(dots)) dots["base_size"] <- 11
  122. do.call("theme_pomological", args = dots)
  123. }
  124. font_urls <- data.frame(
  125. name = c("Mr De Haviland", "Homemade Apple", "Marck Script", "Mr. Bedfort"),
  126. url = c(
  127. "https://fonts.google.com/specimen/Mr+De+Haviland",
  128. "https://fonts.google.com/specimen/Homemade+Apple/",
  129. "https://fonts.google.com/specimen/Marck+Script/",
  130. "https://fonts.google.com/specimen/Mr+Bedfort/"
  131. )
  132. )
  133. check_font <- function(font_name) {
  134. if (!requireNamespace("extrafont", quietly = TRUE)) {
  135. warning("The font \"", font_name, "\" may or may not be installed on your system.",
  136. "Please install the package `extrafont` if you'd like me to be able to check for you.",
  137. call. = FALSE)
  138. } else {
  139. if (!font_name %in% extrafont::fonts()) {
  140. if (font_name %in% font_urls$name) {
  141. warning("Font '", font_name, "' isn't in the extrafonts font list (but it may still work). ",
  142. "If recently installed, you can try running `extrafonts::font_import()`. ",
  143. "To install, visit: ", font_urls[font_urls$name == font_name, "url"],
  144. call. = FALSE)
  145. } else {
  146. warning("Font '", font_name, "' isn't in the extrafonts font list (but it may still work). ",
  147. "If recently installed, you can try running `extrafonts::font_import()`. ",
  148. call. = FALSE)
  149. }
  150. }
  151. }
  152. }