-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.qmd
67 lines (48 loc) · 2.9 KB
/
README.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: ggdiagram
format: gfm
---
```{r}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=ggdiagram)
<!-- badges: end -->
The ggdiagram package takes an object-oriented approach to making diagrams and plots the results using [ggplot2](https://ggplot2.tidyverse.org/).
This package is still experimental, and functions are likely to change in many ways. Every time I run into speed bumps or inconveniences as I use the program in practice, I adapt it to anticipate user needs.
My motivation in making ggdiagram was to find a way to approach the functionality of [TikZ](https://tikz.dev/) but with the flexibility and convenience of R. The ggdiagram package is built atop [S7](https://rconsortium.github.io/S7/) and is integrated with [ggplot2](https://ggplot2.tidyverse.org/), making heavy use of [ggtext](https://wilkelab.org/ggtext/), [ggforce](https://ggforce.data-imaginist.com/), and [ggarrow](https://teunbrand.github.io/ggarrow/).
The ggdiagram package is not a replacement for the standard ggplot2 functions. The underlying *grammar* of ggplot2 is well designed for visualizing data. The ggdiagram functions are best suited for creating a small number of visual objects that interrelate. If anything, they can be thought of as extensions of `ggplot2::annotate`.
## Installation
You can install the development version of ggdiagram like so:
``` r
remotes::install_github("wjschne/ggdiagram")
```
## Example
In @fig_example, we create a circle object with `ob_circle` and a rectangle object with `ob_rectangle`, placing the rectangle such that there is .5 units of separation between the two objects.
The `ggdiagram` function is a a wrapper for `ggplot`, that sets some defaults (e.g., `theme_void`, `coord_equal`, fonts, line size, etc.).
The `ob_*` functions have methods such that they can be added to any ggplot. Under the hood, they are first converted to an appropriate geom and then added to the ggplot object. In this case, an `ob_circle` is converted to a `ggforce:geom_circle`, and an `ob_rectangle` is converted to a `ggforce::geom_shape`.
The `connect` function connects the circle and rectangle at their edges with an arrow (via `ggarrow::geom_arrow_segment`).
```{r fig-example, fig.alt="An arrow points from a circle to a square."}
#| fig-width: 6
#| fig-height: 3.5
#| fig-cap: Connecting a circle and a square with an arrow.
#| dev: ragg_png
#| fig-dpi: 320
library(ggdiagram)
c1 <- ob_circle(radius = 1 / sqrt(pi))
r2 <- ob_rectangle() |>
place(from = c1,
where = "right",
sep = .5)
ggdiagram() +
c1 +
r2 +
connect(c1, r2)
```