🍑 Pomological plot theme for ggplot2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.6KB

  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. ```
  20. <!-- Links -->
  21. [rstudioconf]: https://www.rstudio.com/conference/
  22. [t-aronatkins]: https://twitter.com/aronatkins
  23. [rsconf-slides]: https://github.com/rstudio/rstudio-conf/tree/master/2018/Fruit_For_Thought--Aron_Atkins
  24. [rsconf-video]: https://youtu.be/Ol1FjFR2IMU?t=5h21m15s
  25. [usda-pom]: https://usdawatercolors.nal.usda.gov/pom
  26. [t-pomological]: https://twitter.com/pomological
  27. [magick]: https://cran.r-project.org/web/packages/magick/index.html
  28. 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].
  29. ```{r ggpomological, echo=FALSE, message=FALSE, warning=FALSE}
  30. fruits <- c("Apple", "Apricot", "Banana", "Fig", "Cherry", "Kiwi", "Grape", "Mango", "Papaya", "Orange", "Peach", "Pear")
  31. readr::read_tsv("https://cs.joensuu.fi/sipu/datasets/Compound.txt", col_names = FALSE) %>%
  32. filter(X3 < 10) %>%
  33. mutate(X3 = sample(fruits, length(unique(X3)))[X3]) %>%
  34. {
  35. ggplot(., aes(x = X1, y = X2, color = X3)) +
  36. geom_point() +
  37. labs(x = "Space", y = "Time",
  38. color = "Fruit", title = "ggpomological") +
  39. scale_color_pomological() +
  40. theme_pomological_fancy()
  41. } %>%
  42. paint_pomological(res = 110)
  43. ```
  44. `r knitr::include_graphics(here::here("Readme_files/pom-examples.jpg"))`^[U.S. Department of Agriculture Pomological Watercolor Collection. Rare and Special Collections, National Agricultural Library, Beltsville, MD 20705]
  45. ## Color Palette
  46. 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
  47. ```{r}
  48. scales::show_col(ggpomological:::pomological_palette)
  49. ```
  50. and a few colors for the plot background and decoration
  51. ```{r}
  52. scales::show_col(unlist(ggpomological:::pomological_base))
  53. ```
  54. I've also included a [css file](inst/pomological.css) with the complete collection of color samples.
  55. ## Setup theme and scales
  56. There are three theme-generating functions:
  57. - `theme_pomological()` sets the plot theme to be representative of the paper and styling of the watercolors and includes a paper-colored background,
  58. - `theme_pomological_plain()` has the same styling, just with a transparent (or white) background,
  59. - `theme_pomological_fancy()` has the paper-colored background and defaults to a fancy handwritten font ([Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)).
  60. For color and fill scales, **ggpomological** provides `scale_color_pomological()` and `scale_fill_pomological()`.
  61. In the future, I might revisit this package to
  62. 1. Increase colors in discrete scale
  63. 2. Setup paired color scales. Lots of great color pairs in the extracted colors.
  64. 3. Set up continuous scale colors (we'll see...)
  65. ## Fonts
  66. A handwriting font is needed for the fully authentic pomological look, and I found a few from Google Fonts that fit the bill.
  67. - [Mr. De Haviland](https://fonts.google.com/specimen/Mr+De+Haviland)
  68. - [Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)
  69. - [Marck Script](https://fonts.google.com/specimen/Marck+Script/)
  70. - [Mr. Bedfort](https://fonts.google.com/specimen/Mr+Bedfort/)
  71. Alternatively, use something like [calligrapher.com](https://www.calligraphr.com/) to create your own handwriting font!
  72. But fonts can be painful in R, so the base functions -- `theme_pomological()` and `theme_pomological_plain()` -- don't change the font by default.
  73. 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)`.
  74. ## Add paper background!
  75. **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.
  76. ## Demo!
  77. We'll need ggplot2 (loaded with **ggpomological**) and dplyr
  78. ```r
  79. library(ggpomological)
  80. library(dplyr)
  81. ```
  82. **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.
  83. ### Basic iris plot
  84. ```{r plot-demo}
  85. # Base plot
  86. basic_iris_plot <- ggplot(iris) +
  87. aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
  88. geom_point(size = 2)
  89. # Just your standard Iris plot
  90. basic_iris_plot
  91. # With pomological colors
  92. basic_iris_plot <- basic_iris_plot + scale_color_pomological()
  93. basic_iris_plot
  94. # With pomological theme
  95. basic_iris_plot + theme_pomological()
  96. # With transparent background
  97. basic_iris_plot + theme_pomological_plain()
  98. # Or with "fancy" pomological settings
  99. pomological_iris <- basic_iris_plot + theme_pomological_fancy()
  100. # Painted!
  101. paint_pomological(pomological_iris, res = 110)
  102. ```
  103. ### Stacked bar chart
  104. ```{r plot-bar-chart}
  105. stacked_bar_plot <- ggplot(diamonds) +
  106. aes(price, fill = cut) +
  107. geom_histogram(binwidth = 850) +
  108. xlab('Price (USD)') +
  109. ylab('Count') +
  110. ggtitle("ggpomological") +
  111. scale_x_continuous(label = scales::dollar_format()) +
  112. scale_fill_pomological()
  113. stacked_bar_plot + theme_pomological("Homemade Apple", 16)
  114. paint_pomological(
  115. stacked_bar_plot + theme_pomological_fancy(),
  116. res = 110
  117. )
  118. ```
  119. ### Density Plot
  120. ```{r plot-density}
  121. density_plot <- mtcars %>%
  122. mutate(cyl = factor(cyl)) %>%
  123. ggplot() +
  124. aes(mpg, fill = cyl, color = cyl)+
  125. geom_density(alpha = 0.75) +
  126. labs(fill = 'Cylinders', colour = 'Cylinders', x = 'MPG', y = 'Density') +
  127. scale_color_pomological() +
  128. scale_fill_pomological()
  129. density_plot + theme_pomological("Homemade Apple", 16)
  130. paint_pomological(
  131. density_plot + theme_pomological_fancy(),
  132. res = 110
  133. )
  134. ```
  135. ### Points and lines
  136. Data from the Texas Housing
  137. ```{r plot-full-bar-stack}
  138. big_volume_cities <- txhousing %>%
  139. group_by(city) %>%
  140. summarize(mean_volume = mean(volume, na.rm = TRUE)) %>%
  141. arrange(-mean_volume) %>%
  142. top_n(length(ggpomological:::pomological_palette)) %>%
  143. pull(city)
  144. full_bar_stack_plot <- txhousing %>%
  145. filter(city %in% big_volume_cities) %>%
  146. group_by(city, year) %>%
  147. summarize(mean_volume = mean(volume, na.rm = TRUE)) %>%
  148. ungroup %>%
  149. mutate(city = factor(city, big_volume_cities)) %>%
  150. ggplot() +
  151. aes(year, mean_volume, fill = city, group = city) +
  152. geom_col(position = 'fill', width = 0.9) +
  153. labs(x = 'City', y = 'Mean Volume', color = 'City') +
  154. theme(panel.grid.minor.x = element_blank()) +
  155. scale_fill_pomological()
  156. full_bar_stack_plot + theme_pomological("Homemade Apple", 16)
  157. paint_pomological(
  158. full_bar_stack_plot + theme_pomological_fancy(),
  159. res = 110
  160. )
  161. ```
  162. ### One last plot
  163. Using my own handwriting and the `ggridges` package.
  164. ```{r plot-ridges}
  165. ridges_pomological <- ggplot(diamonds) +
  166. aes(x = carat, y = clarity, color = clarity, fill = clarity) +
  167. ggridges::geom_density_ridges(alpha = 0.75) +
  168. theme_pomological(
  169. base_family = 'gWriting',
  170. base_size = 20,
  171. base_theme = ggridges::theme_ridges()
  172. ) +
  173. scale_fill_pomological() +
  174. scale_color_pomological()
  175. paint_pomological(ridges_pomological, res = 110)
  176. ```