Bläddra i källkod

Add plain theme and restyle theme_pomological a bit

pull/1/head
Garrick Aden-Buie 8 år sedan
förälder
incheckning
27fe2ce7af
17 ändrade filer med 112 tillägg och 46 borttagningar
  1. +1
    -1
      DESCRIPTION
  2. +1
    -0
      NAMESPACE
  3. +54
    -26
      R/theme_pomological.R
  4. +9
    -3
      Readme.Rmd
  5. +15
    -3
      Readme.md
  6. Binär
      Readme_files/figure-gfm/plot-bar-chart-1.png
  7. Binär
      Readme_files/figure-gfm/plot-bar-chart-painted.png
  8. Binär
      Readme_files/figure-gfm/plot-demo-2.png
  9. Binär
      Readme_files/figure-gfm/plot-demo-3.png
  10. Binär
      Readme_files/figure-gfm/plot-demo-4.png
  11. Binär
      Readme_files/figure-gfm/plot-demo-painted.png
  12. Binär
      Readme_files/figure-gfm/plot-density-1.png
  13. Binär
      Readme_files/figure-gfm/plot-density-demo-painted.png
  14. Binär
      Readme_files/figure-gfm/plot-full-bar-stack-1.png
  15. Binär
      Readme_files/figure-gfm/plot-full-bar-stack-painted.png
  16. Binär
      Readme_files/figure-gfm/plot-ridges-painted.png
  17. +32
    -13
      man/theme_pomological.Rd

+ 1
- 1
DESCRIPTION Visa fil

@@ -1,5 +1,5 @@
Package: ggpomological
Version: 0.0.0.9000
Version: 0.0.0.9001
Title: Pomological plot themes for ggplot2
Description: Pomological plot themes and scales for ggplot2 (in progress)
Authors@R: person("Garrick", "Aden-Buie", , "g.adenbuie@gmail.com", c("aut", "cre"))

+ 1
- 0
NAMESPACE Visa fil

@@ -6,3 +6,4 @@ export(scale_colour_pomological)
export(scale_fill_pomological)
export(theme_pomological)
export(theme_pomological_nobg)
export(theme_pomological_plain)

+ 54
- 26
R/theme_pomological.R Visa fil

@@ -4,14 +4,13 @@
#'
#' @references https://usdawatercolors.nal.usda.gov/pom
#' @seealso [ggplot2::theme]
#' @param base_family Base text family
#' @param base_size Base text size
#' @param base_family,base_size Base text family and size
#' @param text.color Color of all text (except axis text, see `axis.text.color`)
#' @param plot.background.color Color of plot background, passed to `plot.background`
#' @param panel.grid.color Color of panel grid, passed to `panel.grid`
#' @param panel.grid.linetype Linetype of panel grid, passed to `panel.grid`
#' @param axis.text.color Color of axis text
#' @param axis.text.size Size of axis text
#' @param panel.border.color Color of plot panel border
#' @param with.panel.grid If `FALSE` gridlines in plot are removed
#' @param panel.grid.color,panel.grid.linetype Color and linetype of panel grid, passed to `panel.grid`
#' @param axis.text.color,axis.text.size Color and size of axis text
#' @param base_theme Starting theme of plot, default is
#' [ggplot2::theme_minimal()]. Any elements set by `theme_pomological()` will
#' overwrite the `base_theme` unless the specific parameter is explicitly set
@@ -25,15 +24,23 @@
#' - [Homemade Apple](https://fonts.google.com/specimen/Homemade+Apple/)
#' - [Amatic SC](https://fonts.google.com/specimen/Amatic+SC/)
#' - [Mr. Bedfort](https://fonts.google.com/specimen/Mr+Bedfort/)
#'
#' Fonts with R are notoriously tricky, so these may not work well for you. If
#' you have installed the fonts but they aren't showing up or working, you can
#' always try running `extrafont::font_import()` or `extrafont::load_fonts()` in
#' the session or RMarkdown document. Or you can use [theme_pomological_plain()].
#'
#' @examples
#' library(ggplot2)
#' basic_iris_plot <- ggplot(iris) +
#' aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
#' geom_point(size = 2)
#' geom_point(size = 2) +
#' # with pomological color scale
#' scale_color_pomological()
#'
#' # Pomological Theme
#' basic_iris_plot + theme_pomological()
#' basic_iris_plot +
#' theme_pomological()
#'
#' # Don't change panel grid color
#' basic_iris_plot +
@@ -44,41 +51,51 @@
#' # White background
#' basic_iris_plot +
#' theme_pomological_nobg()
#'
#' # Plain plot without font or background
#' basic_iris_plot +
#' theme_pomological_plain()
#'
#' @export
theme_pomological <- function(
base_family = 'Homemade Apple',
base_size = 16,
text.color = NULL,
plot.background.color = NULL,
panel.grid.color = NULL,
panel.grid.linetype = 'dashed',
axis.text.color = NULL,
text.color = pomological_base$dark_blue,
plot.background.color = pomological_base$paper,
panel.border.color = pomological_base$light_line,
with.panel.grid = FALSE,
panel.grid.color = pomological_base$light_line,
panel.grid.linetype = "dashed",
axis.text.color = pomological_base$medium_line,
axis.text.size = base_size * 14/16,
base_theme = ggplot2::theme_minimal()
) {
if (!is.null(base_family)) check_font(base_family)
base_theme +
base_theme +
ggplot2::theme(
text = ggplot2::element_text(
family = base_family,
size = base_size,
colour = ifelse(hasArg(text.color), text.color, pomological_base$dark_blue)
colour = text.color
),
plot.background = ggplot2::element_rect(
fill = ifelse(hasArg(plot.background.color), plot.background.color, pomological_base$paper),
fill = plot.background.color,
colour = NA
),
panel.grid = ggplot2::element_line(
colour = ifelse(hasArg(panel.grid.color), panel.grid.color, pomological_base$light_line),
linetype = panel.grid.linetype),
panel.grid.major = ggplot2::element_line(
colour = ifelse(hasArg(panel.grid.color), panel.grid.color, pomological_base$light_line),
colour = panel.grid.color,
linetype = panel.grid.linetype),
panel.border = ggplot2::element_rect(
color = panel.border.color,
fill = NA,
linetype = "solid",
size = 0.75
),
panel.grid.major = if (!with.panel.grid) ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
axis.text = ggplot2::element_text(
colour = ifelse(hasArg(axis.text.color), axis.text.color, pomological_base$medium_line),
colour = axis.text.color,
size = axis.text.size)
)
}
@@ -87,8 +104,19 @@ theme_pomological <- function(
#' @export
theme_pomological_nobg <- function(...) {
dots <- list(...)
dots$plot.background.color <- 'transparent'
do.call('theme_pomological', args = dots)
dots$plot.background.color <- "transparent"
do.call("theme_pomological", args = dots)
}

#' @describeIn theme_pomological A "plain" pomological theme with white
#' background and normal fonts.
#' @export
theme_pomological_plain <- function(...) {
dots <- list(...)
dots$plot.background.color <- "transparent"
if (!"base_family" %in% names(dots)) dots["base_family"] <- ""
if (!"base_size" %in% names(dots)) dots["base_size"] <- 11
do.call("theme_pomological", args = dots)
}

font_urls <- data.frame(
@@ -101,7 +129,7 @@ font_urls <- data.frame(
)

check_font <- function(font_name) {
if (!requireNamespace('extrafont', quietly = TRUE)) {
if (!requireNamespace("extrafont", quietly = TRUE)) {
warning("The font \"", font_name, "\" may or may not be installed on your system.",
"Please install the package `extrafont` if you'd like me to be able to check for you.")
} else {
@@ -109,11 +137,11 @@ check_font <- function(font_name) {
if (font_name %in% font_urls$name) {
warning("Unable to find font '", font_name, "'. ",
"If recently installed, please run `extrafonts::font_import()`. ",
"To install, visit: ", font_urls[font_urls$name == font_name, 'url'])
"To install, visit: ", font_urls[font_urls$name == font_name, "url"])
} else {
warning("Unable to find font '", font_name, "'. ",
"If recently installed, please run `extrafonts::font_import()`. ")
}
}
}
}
}

+ 9
- 3
Readme.Rmd Visa fil

@@ -27,6 +27,8 @@ library(dplyr)

This package provides a ggplot2 theme inspired by the [USDA Pomological Watercolors collection][usda-pom] and by Aron Atkins's ([\@aronatkins][t-aronatkins]) [talk on parameterized RMarkdown][rsconf-video] at [rstudio::conf 2018][rstudioconf].

![](Readme_files/figure-gfm/plot-bar-chart-painted.png)

![](Readme_files/pom-examples.jpg)

## Installation
@@ -57,11 +59,13 @@ I've also included a [css file](inst/pomological.css) with the complete collecti

## Setup theme and scales

There are two theme-generating functions:
There are three theme-generating functions:

- `theme_pomological()` sets the plot theme to be representative of the paper and styling of the watercolors and includes a paper-colored background,

- and `theme_pomological_nobg()` has the same styling, just with a transparent (or white) background.
- `theme_pomological_nobg()` has the same styling, just with a transparent (or white) background,

- `theme_pomological_plain()` has a white background and doesn't change the font unless requested.

A handwriting font is needed for the fully authentic pomological look, and I found a few from Google Fonts that fit the bill.

@@ -112,13 +116,15 @@ basic_iris_plot +
theme_pomological() +
scale_color_pomological()


# With transparent background
pomological_iris <- basic_iris_plot +
theme_pomological_nobg() +
scale_color_pomological()
pomological_iris

# Without fonts
pomological_iris + theme_pomological_plain()

# Painted!
paint_pomological(pomological_iris, res = 110) %>%
magick::image_write("Readme_files/figure-gfm/plot-demo-painted.png")

+ 15
- 3
Readme.md Visa fil

@@ -13,6 +13,8 @@ Aron Atkins’s ([@aronatkins](https://twitter.com/aronatkins)) [talk on
parameterized RMarkdown](https://youtu.be/Ol1FjFR2IMU?t=5h21m15s) at
[rstudio::conf 2018](https://www.rstudio.com/conference/).

![](Readme_files/figure-gfm/plot-bar-chart-painted.png)

![](Readme_files/pom-examples.jpg)

## Installation
@@ -50,14 +52,17 @@ collection of color samples.

## Setup theme and scales

There are two theme-generating functions:
There are three theme-generating functions:

- `theme_pomological()` sets the plot theme to be representative of
the paper and styling of the watercolors and includes a
paper-colored background,

- and `theme_pomological_nobg()` has the same styling, just with a
transparent (or white) background.
- `theme_pomological_nobg()` has the same styling, just with a
transparent (or white) background,

- `theme_pomological_plain()` has a white background and doesn’t
change the font unless requested.

A handwriting font is needed for the fully authentic pomological look,
and I found a few from Google Fonts that fit the bill.
@@ -132,6 +137,13 @@ pomological_iris

![](Readme_files/figure-gfm/plot-demo-3.png)<!-- -->

``` r
# Without fonts
pomological_iris + theme_pomological_plain()
```

![](Readme_files/figure-gfm/plot-demo-4.png)<!-- -->

``` r
# Painted!
paint_pomological(pomological_iris, res = 110) %>%

Binär
Readme_files/figure-gfm/plot-bar-chart-1.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 45KB Width: 768  |  Height: 480  |  Size: 36KB

Binär
Readme_files/figure-gfm/plot-bar-chart-painted.png Visa fil

Before After
Width: 800  |  Height: 500  |  Size: 536KB Width: 800  |  Height: 500  |  Size: 531KB

Binär
Readme_files/figure-gfm/plot-demo-2.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 67KB Width: 768  |  Height: 480  |  Size: 57KB

Binär
Readme_files/figure-gfm/plot-demo-3.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 68KB Width: 768  |  Height: 480  |  Size: 58KB

Binär
Readme_files/figure-gfm/plot-demo-4.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 44KB

Binär
Readme_files/figure-gfm/plot-demo-painted.png Visa fil

Before After
Width: 800  |  Height: 500  |  Size: 542KB Width: 800  |  Height: 500  |  Size: 539KB

Binär
Readme_files/figure-gfm/plot-density-1.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 63KB Width: 768  |  Height: 480  |  Size: 51KB

Binär
Readme_files/figure-gfm/plot-density-demo-painted.png Visa fil

Before After
Width: 800  |  Height: 500  |  Size: 525KB Width: 800  |  Height: 500  |  Size: 520KB

Binär
Readme_files/figure-gfm/plot-full-bar-stack-1.png Visa fil

Before After
Width: 768  |  Height: 480  |  Size: 55KB Width: 768  |  Height: 480  |  Size: 52KB

Binär
Readme_files/figure-gfm/plot-full-bar-stack-painted.png Visa fil

Before After
Width: 800  |  Height: 500  |  Size: 527KB Width: 800  |  Height: 500  |  Size: 526KB

Binär
Readme_files/figure-gfm/plot-ridges-painted.png Visa fil

Before After
Width: 800  |  Height: 500  |  Size: 535KB Width: 800  |  Height: 500  |  Size: 529KB

+ 32
- 13
man/theme_pomological.Rd Visa fil

@@ -3,32 +3,36 @@
\name{theme_pomological}
\alias{theme_pomological}
\alias{theme_pomological_nobg}
\alias{theme_pomological_plain}
\title{Pomological Theme}
\usage{
theme_pomological(base_family = "Homemade Apple", base_size = 16,
text.color = NULL, plot.background.color = NULL,
panel.grid.color = NULL, panel.grid.linetype = "dashed",
axis.text.color = NULL, axis.text.size = base_size * 14/16,
base_theme = ggplot2::theme_minimal())
text.color = pomological_base$dark_blue,
plot.background.color = pomological_base$paper,
panel.border.color = pomological_base$light_line, with.panel.grid = FALSE,
panel.grid.color = pomological_base$light_line,
panel.grid.linetype = "dashed",
axis.text.color = pomological_base$medium_line, axis.text.size = base_size
* 14/16, base_theme = ggplot2::theme_minimal())

theme_pomological_nobg(...)

theme_pomological_plain(...)
}
\arguments{
\item{base_family}{Base text family}

\item{base_size}{Base text size}
\item{base_family, base_size}{Base text family and size}

\item{text.color}{Color of all text (except axis text, see \code{axis.text.color})}

\item{plot.background.color}{Color of plot background, passed to \code{plot.background}}

\item{panel.grid.color}{Color of panel grid, passed to \code{panel.grid}}
\item{panel.border.color}{Color of plot panel border}

\item{panel.grid.linetype}{Linetype of panel grid, passed to \code{panel.grid}}
\item{with.panel.grid}{If \code{FALSE} gridlines in plot are removed}

\item{axis.text.color}{Color of axis text}
\item{panel.grid.color, panel.grid.linetype}{Color and linetype of panel grid, passed to \code{panel.grid}}

\item{axis.text.size}{Size of axis text}
\item{axis.text.color, axis.text.size}{Color and size of axis text}

\item{base_theme}{Starting theme of plot, default is
\code{\link[ggplot2:theme_minimal]{ggplot2::theme_minimal()}}. Any elements set by \code{theme_pomological()} will
@@ -41,6 +45,9 @@ to \code{NULL}.}
\section{Functions}{
\itemize{
\item \code{theme_pomological_nobg}: Pomological theme with white (transparent) background

\item \code{theme_pomological_plain}: A "plain" pomological theme with white
background and normal fonts.
}}

\section{Fonts}{
@@ -53,16 +60,24 @@ Visit the links below to install on your system.
\item \href{https://fonts.google.com/specimen/Amatic+SC/}{Amatic SC}
\item \href{https://fonts.google.com/specimen/Mr+Bedfort/}{Mr. Bedfort}
}

Fonts with R are notoriously tricky, so these may not work well for you. If
you have installed the fonts but they aren't showing up or working, you can
always try running \code{extrafont::font_import()} or \code{extrafont::load_fonts()} in
the session or RMarkdown document. Or you can use \code{\link[=theme_pomological_plain]{theme_pomological_plain()}}.
}

\examples{
library(ggplot2)
basic_iris_plot <- ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
geom_point(size = 2)
geom_point(size = 2) +
# with pomological color scale
scale_color_pomological()

# Pomological Theme
basic_iris_plot + theme_pomological()
basic_iris_plot +
theme_pomological()

# Don't change panel grid color
basic_iris_plot +
@@ -73,6 +88,10 @@ basic_iris_plot +
# White background
basic_iris_plot +
theme_pomological_nobg()
# Plain plot without font or background
basic_iris_plot +
theme_pomological_plain()

}
\references{

Laddar…
Avbryt
Spara