You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/plots.Rmd
+47-12Lines changed: 47 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -20,18 +20,19 @@ knitr::opts_chunk$set(
20
20
21
21
# Introduction
22
22
23
-
This vignette demonstrates pedigree visualisation with `ggPedigree()` from `ggpedigree`.
24
-
Internally, `ggPedigree()` leverages helpers from both {BGmisc} (ped2fam(), recodeSex()) and {ggpedigree} (`calculateCoordinates()`, `calculateConnections()`), and returns a standard `ggplot2` object, so any familiar ggplot syntax can be added afterward.
23
+
This vignette demonstrates pedigree visualization with `ggPedigree()` from `ggpedigree`. Pedigree plots are central to fields like human genetics, behavioral science, and genealogy, where understanding lineage and familial relationships informs diagnosis, analysis, and interpretation.
25
24
26
-
We illustrate usage with two bundled data sets:
25
+
`ggPedigree()` builds on `ggplot2` and `kinship2`, leveraging modular helpers from both `{BGmisc}` and `{ggpedigree}` to streamline plot generation while retaining full aesthetic flexibility.
27
26
28
-
-`potter` – extended wizarding family
27
+
Unlike base R pedigree tools, this package returns a `ggplot2` object, meaning users can customize their output using familiar `ggplot2` syntax, themes, scales, and layering. Throughout this vignette, we walk through basic usage, layout control, status overlays, aesthetic customization, and faceting for multifamily visualization.
29
28
30
-
-`hazard` – historical royalty
29
+
We use two bundled example datasets (from `BGmisc`) to illustrate the package's capabilities. The first dataset, `potter`, contains a fictional wizarding family tree, while the second dataset, `hazard`, includes a a multigenerational historical pedigree with affected/unaffected status.
31
30
32
31
33
32
# Basic usage
34
33
34
+
We begin by loading the required libraries. These include the main plotting package `ggpedigree`, helper utilities from `BGmisc`, and supporting tools for aesthetic and data manipulation.
35
+
35
36
```{r libraries, message=FALSE, warning=FALSE}
36
37
library(ggpedigree) # ggPedigree lives here
37
38
library(BGmisc) # helper utilities & example data
@@ -40,6 +41,9 @@ library(viridis) # viridis for color palettes
40
41
library(tidyverse) # for data wrangling
41
42
```
42
43
44
+
45
+
The simplest usage requires a data frame and column names for family and individual IDs. Here’s a basic pedigree plot using the `potter` data:
46
+
43
47
```{r}
44
48
data("potter")
45
49
ggPedigree(potter,
@@ -48,22 +52,24 @@ ggPedigree(potter,
48
52
)
49
53
```
50
54
51
-
`ggPedigree()` automatically:
55
+
Behind the scenes, `ggPedigree()`:
52
56
53
57
1. reshapes the data by family (`ped2fam()`),
54
58
55
-
2. recodes sex (`recodeSex()`),
59
+
2. recodes sex (e.g., 0/1/NA) into semantic labels via `recodeSex()`
60
+
61
+
3. extracts a layout grid with `calculateCoordinates()`
56
62
57
-
3. computes node layout (`calculateCoordinates()`), and
63
+
4. builds connection segments for spouses, siblings, parents, and offspring with `calculateConnections()`
58
64
59
-
4. draws spouse, parent, sibling, and offspring links (`calculateConnections()`).
65
+
The result is returned as a `ggplot2` object, which allows immediate post-processing with standard `ggplot` syntax.
60
66
61
-
The result is a `ggplot2` object, so you can add any ggplot syntax to it.
62
67
63
68
64
69
# Customizing the plot
65
70
66
-
All aesthetics reside in a config list; anything you omit falls back to the default.
71
+
Users can control aesthetics via the `config` list.
72
+
Most appearance settings are managed through a `config` list. If any options are omitted, they fall back to predefined defaults. For example, we can turn off sex-based fill color and assign specific colors to connection segments:
67
73
68
74
```{r}
69
75
ggPedigree(
@@ -81,7 +87,8 @@ ggPedigree(
81
87
)
82
88
```
83
89
84
-
Because the result is just a ggplot object, regular layering applies:
90
+
As with any `ggplot2` object, further theming and labeling can be applied using layers or themes. For instance:
91
+
85
92
86
93
```{r}
87
94
ggPedigree(potter,
@@ -90,11 +97,39 @@ ggPedigree(potter,
90
97
) +
91
98
theme_bw(base_size = 12)
92
99
```
100
+
This can be particularly useful for matching house styles in academic publications or removing clutter for presentations.
93
101
94
102
95
103
96
104
# Additional customization
97
105
106
+
In many applied settings, pedigrees include binary phenotypes such as affected/unaffected status. To incorporate this, use the `status_col` argument to point to a column containing the relevant variable.
107
+
108
+
```{r}
109
+
data("hazard")
110
+
111
+
p <- ggPedigree(
112
+
hazard,
113
+
famID = "famID",
114
+
personID = "ID",
115
+
status_col = "affected",
116
+
config = list(
117
+
code_male = 0,
118
+
sex_color = TRUE,
119
+
affected = TRUE,
120
+
unaffected = FALSE,
121
+
affected_shape = 4
122
+
)
123
+
)
124
+
125
+
p
126
+
```
127
+
128
+
The function will automatically assign different shapes to affected individuals. If the `status_col` isn't already a factor, `ggPedigree()` will coerce it. You can explicitly set what values correspond to "affected" and "unaffected" via the `config` list.
129
+
130
+
If you turn off sex-based coloring, `ggPedigree()` will apply default `ggplot2` fill scales based on status:
131
+
132
+
98
133
You can also add affected/unaffected status to the plot. The `status_col` argument allows you to specify a column in your data frame that contains the affected/unaffected status of each individual. This column should be a factor or character vector, and `ggPedigree()` will automatically recode it to a factor if necessary.
99
134
100
135
When you add the `status_col` argument, `ggPedigree()` will automatically color the affected individuals in one color and the unaffected individuals in another. You can indicate what the values of the affected/unaffected status are in your data frame using the `affected` and `unaffected` arguments in the `config` list. By default, `ggPedigree()` will overlay the affected individuals with a different shape, but you can customize this as well.
0 commit comments