🍑 Pomological plot theme for ggplot2
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

250 Zeilen
7.9KB

  1. ---
  2. title: "ggpomological"
  3. author: "Garrick Aden-Buie"
  4. date: "`r Sys.Date()`"
  5. output: rmarkdown::html_vignette
  6. vignette: >
  7. %\VignetteIndexEntry{ggpomological}
  8. %\VignetteEngine{knitr::rmarkdown}
  9. %\VignetteEncoding{UTF-8}
  10. ---
  11. ```{r setup, include = FALSE}
  12. knitr::opts_chunk$set(
  13. collapse = TRUE,
  14. comment = "#>"
  15. )
  16. knitr::opts_chunk$set(echo = TRUE, fig.width=8, fig.height=5)
  17. library(ggpomological)
  18. library(dplyr)
  19. pom_examples_path <- if (!exists("README")) "../man/figures/pom-examples.jpg" else "man/figures/pom-examples.jpg"
  20. ```
  21. <!-- Links -->
  22. [rstudioconf]: https://www.rstudio.com/conference/
  23. [t-aronatkins]: https://twitter.com/aronatkins
  24. [rsconf-slides]: https://github.com/rstudio/rstudio-conf/tree/master/2018/Fruit_For_Thought--Aron_Atkins
  25. [rsconf-video]: https://youtu.be/Ol1FjFR2IMU?t=5h21m15s
  26. [usda-pom]: https://usdawatercolors.nal.usda.gov/pom
  27. [t-pomological]: https://twitter.com/pomological
  28. [magick]: https://cran.r-project.org/web/packages/magick/index.html
  29. This package provides a ggplot2 theme inspired by the [USDA Pomological Watercolors collection][usda-pom] and by Aron Atkins's ([&commat;aronatkins][t-aronatkins]) [talk on parameterized RMarkdown][rsconf-video] at [rstudio::conf 2018][rstudioconf].
  30. ```{r ggpomological, echo=FALSE, message=FALSE, warning=FALSE}
  31. fruits <- c("Apple", "Apricot", "Banana", "Fig", "Cherry", "Kiwi", "Grape", "Mango", "Papaya", "Orange", "Peach", "Pear")
  32. # https://cs.joensuu.fi/sipu/datasets/Compound.txt
  33. readr::read_tsv(system.file("compound.txt", package = "ggpomological"), col_names = FALSE) %>%
  34. filter(X3 < 10) %>%
  35. mutate(X3 = sample(fruits, length(unique(X3)))[X3]) %>%
  36. {
  37. ggplot(., aes(x = X1, y = X2, color = X3)) +
  38. geom_point() +
  39. labs(x = "Space", y = "Time",
  40. color = "Fruit", title = "ggpomological") +
  41. scale_color_pomological() +
  42. theme_pomological_fancy()
  43. } %>%
  44. paint_pomological(res = 110)
  45. ```
  46. `r knitr::include_graphics(pom_examples_path)`^[U.S. Department of Agriculture Pomological Watercolor Collection. Rare and Special Collections, National Agricultural Library, Beltsville, MD 20705]
  47. ## Color Palette
  48. The colors for this theme were drawn from many images from the [USDA Pomological Watercolors collection][usda-pom], I chose just a few that I thought worked well together for color and fill scales
  49. ```{r}
  50. scales::show_col(ggpomological:::pomological_palette)
  51. ```
  52. and a few colors for the plot background and decoration
  53. ```{r}
  54. scales::show_col(unlist(ggpomological:::pomological_base))
  55. ```
  56. I've also included a [css file](inst/pomological.css) with the complete collection of color samples.
  57. ## Setup theme and scales
  58. There are three theme-generating functions:
  59. - `theme_pomological()` sets the plot theme to be representative of the paper and styling of the watercolors and includes a paper-colored background,
  60. - `theme_pomological_plain()` has the same styling, just with a transparent (or white) background,
  61. - `theme_pomological_fancy()` has the paper-colored background and defaults to a fancy handwritten font ([Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)).
  62. For color and fill scales, **ggpomological** provides `scale_color_pomological()` and `scale_fill_pomological()`.
  63. In the future, I might revisit this package to
  64. 1. Increase colors in discrete scale
  65. 2. Setup paired color scales. Lots of great color pairs in the extracted colors.
  66. 3. Set up continuous scale colors (we'll see...)
  67. ## Fonts
  68. A handwriting font is needed for the fully authentic pomological look, and I found a few from Google Fonts that fit the bill.
  69. - [Mr. De Haviland](https://fonts.google.com/specimen/Mr+De+Haviland)
  70. - [Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)
  71. - [Marck Script](https://fonts.google.com/specimen/Marck+Script/)
  72. - [Mr. Bedfort](https://fonts.google.com/specimen/Mr+Bedfort/)
  73. Alternatively, use something like [calligrapher.com](https://www.calligraphr.com/) to create your own handwriting font!
  74. But fonts can be painful in R, so the base functions -- `theme_pomological()` and `theme_pomological_plain()` -- don't change the font by default.
  75. To opt into the full pomological effect, use `theme_pomological_fancy()` which is just an alias for `theme_pomological(base_family = "Homemade Apple", base_size = 16)`.
  76. ## Add paper background!
  77. **ggpomological** also provides a function named `paint_pomological` that uses the [`magick`][magick] package to add a pomological watercolor paper background and a subtle texture overlay.
  78. ## Demo!
  79. We'll need ggplot2 (loaded with **ggpomological**) and dplyr
  80. ```r
  81. library(ggpomological)
  82. library(dplyr)
  83. ```
  84. **Warning**: If you don't have the [above fonts](#fonts) installed, you'll get an error message with a lot of warnings when running the below examples. Just replace `theme_pomological("Homemade Apple", 16)` with `theme_pomological()` for the basic theme without the crazy fonts.
  85. ### Basic iris plot
  86. ```{r plot-demo}
  87. # Prep msleep data
  88. msleep <- ggplot2::msleep[, c("vore", "sleep_rem", "sleep_total")]
  89. msleep <- msleep[complete.cases(msleep), ]
  90. msleep$vore <- paste0(msleep$vore, "vore")
  91. # Base plot
  92. basic_msleep_plot <- ggplot(msleep) +
  93. aes(x = sleep_rem, y = sleep_total, color = vore) +
  94. geom_point(size = 2) +
  95. labs(color = NULL)
  96. # Just your standard ggplot
  97. basic_msleep_plot
  98. # With pomological colors
  99. basic_msleep_plot <- basic_msleep_plot + scale_color_pomological()
  100. basic_msleep_plot
  101. # With pomological theme
  102. basic_msleep_plot + theme_pomological()
  103. # With transparent background
  104. basic_msleep_plot + theme_pomological_plain()
  105. # Or with "fancy" pomological settings
  106. pomological_msleep <- basic_msleep_plot + theme_pomological_fancy()
  107. # Painted!
  108. paint_pomological(pomological_msleep, res = 110)
  109. ```
  110. ### Stacked bar chart
  111. ```{r plot-bar-chart}
  112. stacked_bar_plot <- ggplot(diamonds) +
  113. aes(price, fill = cut) +
  114. geom_histogram(binwidth = 850) +
  115. xlab('Price (USD)') +
  116. ylab('Count') +
  117. ggtitle("ggpomological") +
  118. scale_x_continuous(labels = scales::dollar_format()) +
  119. scale_fill_pomological()
  120. stacked_bar_plot + theme_pomological("Homemade Apple", 16)
  121. paint_pomological(
  122. stacked_bar_plot + theme_pomological_fancy("Homemade Apple"),
  123. res = 110
  124. )
  125. ```
  126. ### Density Plot
  127. ```{r plot-density}
  128. density_plot <- mtcars %>%
  129. mutate(cyl = factor(cyl)) %>%
  130. ggplot() +
  131. aes(mpg, fill = cyl, color = cyl)+
  132. geom_density(alpha = 0.75) +
  133. labs(fill = 'Cylinders', colour = 'Cylinders', x = 'MPG', y = 'Density') +
  134. scale_color_pomological() +
  135. scale_fill_pomological()
  136. density_plot + theme_pomological("Homemade Apple", 16)
  137. paint_pomological(
  138. density_plot + theme_pomological_fancy(),
  139. res = 110
  140. )
  141. ```
  142. ### Points and lines
  143. Data from the Texas Housing
  144. ```{r plot-full-bar-stack}
  145. big_volume_cities <- txhousing %>%
  146. group_by(city) %>%
  147. summarize(mean_volume = mean(volume, na.rm = TRUE)) %>%
  148. arrange(-mean_volume) %>%
  149. top_n(length(ggpomological:::pomological_palette)) %>%
  150. pull(city)
  151. full_bar_stack_plot <- txhousing %>%
  152. filter(city %in% big_volume_cities) %>%
  153. group_by(city, year) %>%
  154. summarize(mean_volume = mean(volume, na.rm = TRUE)) %>%
  155. ungroup %>%
  156. mutate(city = factor(city, big_volume_cities)) %>%
  157. ggplot() +
  158. aes(year, mean_volume, fill = city, group = city) +
  159. geom_col(position = 'fill', width = 0.9) +
  160. labs(x = 'City', y = 'Mean Volume', color = 'City') +
  161. theme(panel.grid.minor.x = element_blank()) +
  162. scale_fill_pomological()
  163. full_bar_stack_plot + theme_pomological("Homemade Apple", 16)
  164. paint_pomological(
  165. full_bar_stack_plot + theme_pomological_fancy(),
  166. res = 110
  167. )
  168. ```
  169. ### One last plot
  170. Using my own handwriting and the `ggridges` package.
  171. ```{r plot-ridges}
  172. ridges_pomological <- ggplot(diamonds) +
  173. aes(x = carat, y = clarity, color = clarity, fill = clarity) +
  174. ggridges::geom_density_ridges(alpha = 0.75) +
  175. theme_pomological(
  176. base_family = 'gWriting',
  177. base_size = 20,
  178. base_theme = ggridges::theme_ridges()
  179. ) +
  180. scale_fill_pomological() +
  181. scale_color_pomological()
  182. paint_pomological(ridges_pomological, res = 110)
  183. ```