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

148 lines
5.2KB

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