---
title: "Single SVG with multiple aesthetics - Mushroom plot"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Single SVG with multiple aesthetics - Mushroom plot}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(ggplot2)
library(ggsvg)
```
## Mushroom plot
A mushroom plot is a style of plot consistning of two semi-circles which can be
sized/styled independently to represent two quantities at a single graph
location.
#### Create the base SVG
Note: Semicircles are drawn with path arcs in SVG and aren't that intuitive.
```{r fig.height=1.5}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Hand craft two semi-circles using SVG path arcs
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
semicircles_svg <- '
'
grid::grid.draw( svg_to_rasterGrob(semicircles_svg) )
```
#### Create Parameterised SVG
Add `{{}}` glue string locations and test it out
```{r fig.height=1.5}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Parameterise the radii for use with glue
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
semicircles_svg <- '
'
top_radius <- 40
bot_radius <- 30
final_svg <- glue::glue(semicircles_svg, .open = "{{", .close = "}}")
grid::grid.draw( svg_to_rasterGrob(final_svg))
```
#### Create a `ggsvg` plot mapping 2 different quantities to the size
```{r fig.width=6, fig.height=4}
N <- 20
data <- data.frame(
x = runif(N),
y = runif(N),
q1 = runif(N),
q2 = runif(N),
stringsAsFactors = FALSE
)
ggplot(data) +
geom_point_svg(
aes(x, y, top_radius = q1, bot_radius = q2),
svg = semicircles_svg,
defaults = list(top_radius = 50, bot_radius = 50),
size = 10
) +
scale_svg_size('top_radius', range = c(30, 50), guide = 'none') +
scale_svg_size('bot_radius', range = c(30, 50), guide = 'none') +
theme_bw() +
labs(
title = "{ggsvg} Multiple continuous aesthetics"
)
```