🍑 Pomological plot theme for ggplot2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

165 lines
6.4KB

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