-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathREADME.Rmd
173 lines (130 loc) · 6.07 KB
/
README.Rmd
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
output:
md_document:
variant: gfm
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# collapsibleTree `r packageVersion("collapsibleTree")`
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/collapsibleTree)](https://cran.r-project.org/package=collapsibleTree) [![CRAN downloads](http://cranlogs.r-pkg.org/badges/collapsibleTree)](https://cran.r-project.org/package=collapsibleTree)
collapsibleTree is an R [htmlwidget](http://www.htmlwidgets.org/) that allows you to create interactive collapsible Reingold-Tilford tree diagrams using D3.js, adapted from Mike Bostock's [example](https://bl.ocks.org/mbostock/4339083). Turn your data frame into a hierarchical visualization without worrying about nested lists or JSON objects!
If you're using [Shiny](https://shiny.posit.co/), you can bind the most recently clicked node to a Shiny input, allowing for easier interaction with complex nested objects. The input will return a named list containing the most recently selected node, as well as all of its parents. See the [Shiny example](https://adeelk93.shinyapps.io/collapsibletree/) for more info.
## Installation
```{r eval=FALSE}
# Install package from CRAN:
install.packages("collapsibleTree")
# Alternately, install the latest development version from GitHub:
# install.packages("devtools")
devtools::install_github("AdeelK93/collapsibleTree")
```
[Changelog can be found here](https://github.com/AdeelK93/collapsibleTree/releases).
## Usage
When working with data in R, it makes sense (at least to me) to represent everything as a data frame. I'm a big fan of [tidy data](https://cran.r-project.org/package=tidyr/vignettes/tidy-data.html), but this structure does not lend itself to easily designing hierarchical networks.
collapsibleTree uses [data.tree](https://cran.r-project.org/package=data.tree/vignettes/data.tree.html) to handle all of that, freeing you from a lot of recursive list construction.
[Click here](https://adeelk93.github.io/collapsibleTree/) to see some interactive charts.
```{r eval=FALSE}
library(collapsibleTree)
collapsibleTree(warpbreaks, c("wool", "tension", "breaks"))
```
[![Collapsible Tree](README-example-1.PNG)](https://adeelk93.github.io/collapsibleTree/)
The color of each node can be customized to draw attention to the levels of hierarchy. Thanks to Ryan Derickson for the implementation idea! Colors can be constants or generated from a gradient function.
```{r eval=FALSE}
# Data from US Forest Service DataMart
species <- read.csv("https://apps.fs.usda.gov/fia/datamart/CSV/REF_SPECIES_GROUP.csv")
collapsibleTree(
species,
hierarchy = c("REGION", "CLASS", "NAME"),
fill = c(
# The root
"seashell",
# Unique regions
rep("brown", length(unique(species$REGION))),
# Unique classes per region
rep("khaki", length(unique(paste(species$REGION, species$CLASS)))),
# Unique names per region
rep("forestgreen", length(unique(paste(species$NAME, species$REGION))))
)
)
```
[![Collapsible Tree Colored](README-example-2.PNG)](https://adeelk93.github.io/collapsibleTree/)
Gradients can be mapped to a column in the data frame to help visualize relative weightings of nodes. Node weighting can also be mapped to a tooltip.
```{r eval=FALSE}
collapsibleTreeSummary(
warpbreaks,
c("wool", "tension", "breaks"),
attribute = "breaks",
maxPercent = 50
)
```
[![Collapsible Tree Gradient](README-example-3.PNG)](https://adeelk93.github.io/collapsibleTree/)
Likewise, node size can also be mapped to a column in the data frame to help visualize relative weightings of nodes.
```{r eval=FALSE}
collapsibleTreeSummary(
warpbreaks,
c("wool", "tension", "breaks"),
attribute = "breaks",
maxPercent = 50,
nodeSize = "breaks",
collapsed = FALSE
)
```
[![Collapsible Tree Gradient](README-example-4.PNG)](https://adeelk93.github.io/collapsibleTree/)
Parent-child relationships can be mapped to the tree to give more customizability for each node, such as passing custom html elements to each node.
```{r eval=FALSE}
# Create a simple org chart
org <- data.frame(
Manager = c(
NA, "Ana", "Ana", "Bill", "Bill", "Bill", "Claudette", "Claudette", "Danny",
"Fred", "Fred", "Grace", "Larry", "Larry", "Nicholas", "Nicholas"
),
Employee = c(
"Ana", "Bill", "Larry", "Claudette", "Danny", "Erika", "Fred", "Grace",
"Henri", "Ida", "Joaquin", "Kate", "Mindy", "Nicholas", "Odette", "Peter"
),
Title = c(
"President", "VP Operations", "VP Finance", "Director", "Director", "Scientist",
"Manager", "Manager", "Jr Scientist", "Operator", "Operator", "Associate",
"Analyst", "Director", "Accountant", "Accountant"
)
)
# Add in colors and sizes
org$Color <- org$Title
levels(org$Color) <- colorspace::rainbow_hcl(11)
# Use unsplash api to add in random photos to tooltip
org$tooltip <- paste0(
org$Employee,
"<br>Title: ",
org$Title,
"<br><img src='https://source.unsplash.com/collection/385548/150x100'>"
)
collapsibleTreeNetwork(
org,
attribute = "Title",
fill = "Color",
nodeSize = "leafCount",
tooltipHtml = "tooltip"
)
```
[![Collapsible Tree Network](README-example-5.PNG)](https://adeelk93.github.io/collapsibleTree/)
## Shiny Interaction
An interactive Shiny demo is also included. For example, you could use the collapsibleTree htmlwidget to select a portion of a larger categorical dataset, with your filter being as deep or shallow as you'd prefer. You can find a live demo [here](https://adeelk93.shinyapps.io/collapsibletree/), or run the included examples locally.
```{r eval=FALSE}
# Basic Shiny Interaction
shiny::runApp(system.file("examples/02shiny", package = "collapsibleTree"))
# Interactive Gradient Mapping
shiny::runApp(system.file("examples/03shiny", package = "collapsibleTree"))
```
## Issues and Suggestions
Feel free to submit an issue if you run into any bugs or have any feature suggestions! Would love to hear your comments.
## Test Results
```{r}
library(collapsibleTree)
date()
testthat::test_dir("tests/testthat", reporter = testthat::SummaryReporter)
```