🍑 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.

156 lines
6.1KB

  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. #' # With fonts (manual)
  52. #' basic_iris_plot +
  53. #' theme_pomological("Homemade Apple", 16)
  54. #'
  55. #' # Or with fancy alias (same as previous)
  56. #' basic_iris_plot +
  57. #' theme_pomological_fancy()
  58. #'
  59. #' # Plain plot without font or background
  60. #' basic_iris_plot +
  61. #' theme_pomological_plain()
  62. #'
  63. #' @export
  64. theme_pomological <- function(
  65. base_family = NULL,
  66. base_size = 11,
  67. text.color = pomological_base$dark_blue,
  68. plot.background.color = pomological_base$paper,
  69. panel.border.color = pomological_base$light_line,
  70. with.panel.grid = FALSE,
  71. panel.grid.color = pomological_base$light_line,
  72. panel.grid.linetype = "dashed",
  73. axis.text.color = pomological_base$medium_line,
  74. axis.text.size = base_size * 3/4,
  75. base_theme = ggplot2::theme_minimal()
  76. ) {
  77. if (!is.null(base_family)) check_font(base_family)
  78. base_theme +
  79. ggplot2::theme(
  80. text = ggplot2::element_text(
  81. family = if (!is.null(base_family)) base_family,
  82. size = base_size,
  83. colour = text.color
  84. ),
  85. plot.background = ggplot2::element_rect(
  86. fill = plot.background.color,
  87. colour = NA
  88. ),
  89. panel.grid = ggplot2::element_line(
  90. colour = panel.grid.color,
  91. linetype = panel.grid.linetype),
  92. panel.border = ggplot2::element_rect(
  93. color = panel.border.color,
  94. fill = NA,
  95. linetype = "solid",
  96. size = 0.75
  97. ),
  98. panel.grid.major = if (!with.panel.grid) ggplot2::element_blank(),
  99. panel.grid.minor = ggplot2::element_blank(),
  100. axis.text = ggplot2::element_text(
  101. colour = axis.text.color,
  102. size = axis.text.size)
  103. )
  104. }
  105. #' @describeIn theme_pomological Pomological theme with white (transparent) background
  106. #' @export
  107. theme_pomological_nobg <- function(..., plot.background.color = "transparent") {
  108. theme_pomological(plot.background.color = plot.background.color, ...)
  109. }
  110. #' @describeIn theme_pomological A "plain" pomological theme with white
  111. #' background and normal fonts.
  112. #' @export
  113. theme_pomological_plain <- function(base_family = "", base_size = 11, plot.background.color = "transparent", ...) {
  114. theme_pomological(base_family, base_size, plot.background.color = plot.background.color, ...)
  115. }
  116. #' @describeIn theme_pomological A "fancy" pomological theme with fancy fonts
  117. #' @export
  118. theme_pomological_fancy <- function(base_family = "Homemade Apple", base_size = 16, ...) {
  119. theme_pomological(base_family, base_size, ...)
  120. }
  121. font_urls <- data.frame(
  122. name = c("Mr De Haviland", "Homemade Apple", "Marck Script", "Mr. Bedfort"),
  123. url = c(
  124. "https://fonts.google.com/specimen/Mr+De+Haviland",
  125. "https://fonts.google.com/specimen/Homemade+Apple/",
  126. "https://fonts.google.com/specimen/Marck+Script/",
  127. "https://fonts.google.com/specimen/Mr+Bedfort/"
  128. )
  129. )
  130. check_font <- function(font_name) {
  131. if (!requireNamespace("extrafont", quietly = TRUE)) {
  132. warning("The font \"", font_name, "\" may or may not be installed on your system.",
  133. "Please install the package `extrafont` if you'd like me to be able to check for you.",
  134. call. = FALSE)
  135. } else {
  136. if (!font_name %in% extrafont::fonts()) {
  137. if (font_name %in% font_urls$name) {
  138. warning("Font '", font_name, "' isn't in the extrafont font list (but it may still work). ",
  139. "If recently installed, you can try running `extrafont::font_import()`. ",
  140. "To install, visit: ", font_urls[font_urls$name == font_name, "url"],
  141. call. = FALSE)
  142. } else {
  143. warning("Font '", font_name, "' isn't in the extrafont font list (but it may still work). ",
  144. "If recently installed, you can try running `extrafont::font_import()`. ",
  145. call. = FALSE)
  146. }
  147. }
  148. }
  149. }