Browse Source

vignettes

master
boB Rudis 5 years ago
parent
commit
897c706993
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 4
      .Rbuildignore
  2. 3
      NAMESPACE
  3. 26
      R/fontawesome.R
  4. 20
      man/fa5_fonts.Rd
  5. 150
      vignettes/building-pictograms.Rmd
  6. 2
      waffle.Rproj

4
.Rbuildignore

@ -1,8 +1,8 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^.*Rmd$
^README.*$
^\.git
^\.git$
^\.vscode$
^cran-comments\.md$
^CONDUCT\.md$

3
NAMESPACE

@ -4,6 +4,9 @@ export(GeomPictogram)
export(GeomWaffle)
export(StatWaffle)
export(draw_key_pictogram)
export(fa5_brand)
export(fa5_fonts)
export(fa5_solid)
export(fa_grep)
export(fa_list)
export(geom_pictogram)

26
R/fontawesome.R

@ -50,3 +50,29 @@ install_fa_fonts <- function() {
"your system."
)
}
#' Font Awesome 5 easy use font name objets
#' @name fa5_fonts
#' @export
NULL
#' @description `fa5_solid` is shorthand for "`FontAwesome5Free-Solid`"
#' @rdname fa5_fonts
#' @docType data
#' @export
fa5_solid <- "FontAwesome5Free-Solid"
#' @description `fa5_brand` is shorthand for "`FontAwesome5Brands-Regular`"
#' @rdname fa5_fonts
#' @docType data
#' @export
fa5_brand <- "FontAwesome5Brands-Regular"

20
man/fa5_fonts.Rd

@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fontawesome.R
\docType{data}
\name{fa5_fonts}
\alias{fa5_fonts}
\alias{fa5_solid}
\alias{fa5_brand}
\title{Font Awesome 5 easy use font name objets}
\format{An object of class \code{character} of length 1.}
\usage{
fa5_solid
fa5_brand
}
\description{
`fa5_solid` is shorthand for "`FontAwesome5Free-Solid`"
`fa5_brand` is shorthand for "`FontAwesome5Brands-Regular`"
}
\keyword{datasets}

150
vignettes/building-pictograms.Rmd

@ -0,0 +1,150 @@
---
title: "Building Pictograms"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Building Pictograms}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r top, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE, message = FALSE, warning = FALSE,
fig.width = 8, fig.height = 6, out.width = "100%"
)
```
```{r setup}
library(waffle)
library(hrbrthemes)
library(extrafont)
library(dplyr)
```
[Isotype](https://en.wikipedia.org/wiki/Isotype_(picture_language)) [pictograms](https://en.wikipedia.org/wiki/Pictogram) are _kinda_ like waffle charts but can also convey additional meaning through conveys its meaning through the resemblance to a physical object.
The current iteration of the {`waffle`} package makes it possible to make isotype pictograms through the use of [Font Awesome 5](https://fontawesome.com/) glyph fonts. Specifically, there is support for the [solid](https://fontawesome.com/icons?d=gallery&s=solid) and [brands](https://fontawesome.com/icons?d=gallery&s=brands) Font Awesome 5 variants. (We'll be abbreviated "Font Awesome 5" as just "FA" from this point forward.)
To use FA fonts you need to install them onto your system and then make them available to R. To make the first step in the process easier the package includes an `install_fa_fonts()` function which will present the location of the FA TrueType fonts (ttf) that come with the package. Navigate there, install them however you do that on your operating system, them come back to R and run `extrafont::font_import()`. The {`extrafont`} package is a dependency of this package so it should have come along for the ride when you installed {`waffle`}.
To see if that incantation worked, you can do:
```{r ef}
extrafont::loadfonts(quiet = TRUE)
extrafont::fonttable() %>%
as_tibble() %>%
filter(grepl("Awesom", FamilyName)) %>%
select(afmfile, FullName, FamilyName, FontName)
```
Because it's 2019 and fonts are still daftly difficult across operating systems you may have different `FontName` or `FamilyName` values for each. If so, you won't be able to use the shortcut names and will have to work some things out on your own or file an issue so I can try to account for your setup in the package itself.
While FA is great and provides a clever way to get glyphs into R charts without too many machinations a major downside is that Unicode values are used to handle the glyph mappings. Typing Unicode values is seriously not fun regardless of context and it's also not cool that you have to remember the glyph short name and also the Unicode value. We've tried to alieviate some of this pain in a few ways.
First, we've provided the function `fa_list()` which presents an `htmlwidget` with the short name, which type (brand or solid) it is and also a small image of the font itself. Rather than demonstrate that (and put 139 SVG files in the generated Rmd vignette and kill CRAN's servers) we'll demonstrate a sibling function --- `fa_grep()` --- which lets you do the search before presenting the widget. So, say you wanted to make an isotype pictogram that uses a rocket. You can do something like:
```{r rocket}
fa_grep("rocket")
```
which will present a widget with the available choices. Now, you only need to remember 2 things, the "`rocket`" short name and that it's in the `solid` FA font package.
A more practical example may be that you're trying to show proportions of consumption of three food areas: fruit, sandwiches, and pizza. We can look for available glyphs by paging or guessing some good keywords to grep for:
```{r food}
fa_grep("bread|pizza|apple|pear|peach|lemon|sandwich")
```
We'll use `apple-alt`, `bread-slice` and `pizza-slice`. Now, we just need some data.
```{r pict-data}
tibble(
food_group = factor(
c("Fruit", "Sandwiches", "Pizza"),
levels=c("Fruit", "Sandwiches", "Pizza")
),
consumption = c(5, 20, 52)
) -> xdf
xdf
```
Here's what a traditional waffle chart might look like for that:
```{r waf1}
ggplot(xdf, aes(fill = food_group, values = consumption)) +
geom_waffle() +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
```
Really, you should be making proportional waffle charts since the whole point is to compare parts of a while and we're pretty good (as humans) of doing that in a 10x10 matrix. The `geom_waffle()` function can do that for us for free:
```{r waf2}
ggplot(xdf, aes(fill = food_group, values = consumption)) +
geom_waffle(n_rows = 10, make_proportional = TRUE) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
```
But, this is food and it's fun to play with our food (as we all learned as kids) so let's try turning the boring waffle chart into an pictogram. There are two key {`ggplot2`} components that come with the {`waffle`} package that we _need to use every time we make a pictogram`!!:
- `geom_pictogram()` (duh)
- `scale_label_pictogram()` (which helps us translate factor level names into FA glyphs)
Here's how we do that (annotated with comments in the code block:
```{r pg1, fig.width=8, fig.height=6}
ggplot(xdf, aes(label = food_group, values = consumption)) +
geom_pictogram(n_rows = 10, make_proportional = TRUE, color = "black") +
scale_label_pictogram(
name = NULL,
values = c(
Fruit = "apple-alt",
Sandwiches = "bread-slice",
Pizza = "pizza-slice"
)
) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle() +
theme(legend.key.height = unit(2.25, "line")) +
theme(legend.text = element_text(size = 10, hjust = 0, vjust = 0.75))
```
The extra `theme()` components may be of use to you to help get your output to be decent. Combining these FA glyphs with regular text is a bit tricky and you may need to fiddle with sizing to get the exact look you're going for.
That pictogram is fine, but it could use some color. Let's add some in:
```{r pg2, fig.width=8, fig.height=6}
ggplot(xdf, aes(label = food_group, values = consumption, color = food_group)) +
geom_pictogram(n_rows = 10, make_proportional = TRUE) +
scale_color_manual(
name = NULL,
values = c(
Fruit = "#a40000",
Sandwiches = "#c68958",
Pizza = "#ae6056"
)
) +
scale_label_pictogram(
name = NULL,
values = c(
Fruit = "apple-alt",
Sandwiches = "bread-slice",
Pizza = "pizza-slice"
)
) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle() +
theme(legend.key.height = unit(2.25, "line")) +
theme(legend.text = element_text(size = 10, hjust = 0, vjust = 0.75))
```
All the same powerful features of `geom_waffle()` are available to `geom_pictogram()` (including faceting/etc) but you should use them carefully, sparingly, and wisely. Pictures can help tell a data story but pictures can also distract from the data story.

2
waffle.Rproj

@ -19,4 +19,4 @@ PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageCheckArgs: --as-cran
PackageRoxygenize: rd,collate,namespace,vignette
PackageRoxygenize: rd,collate,namespace

Loading…
Cancel
Save