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

119 lines
4.3KB

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