forked from ameliaritger/mbon-shiny-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sankey.Rmd
69 lines (58 loc) · 2.67 KB
/
sankey.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
---
title: "Sankey diagram practice"
author: "Amelia Ritger"
date: "4/23/2020"
output: html_document
---
Create a Senke diagram for the community data
```{r}
library(d3Network)
reef_summary <- reef_tidy %>%
filter(binary > "0") %>% #filter out species not present
filter(location=="Solimar") %>%
group_by(phylum, genus) %>% #group by location, then lat/long (and then orientation for vert/horizontal comparisons)
summarize(`mean abundance` = mean(value), #get the mean count
median_count = median(value),
sd_count = sd(value), #get the s.d. count
iqr = IQR(value), #get the interquartile range for the count
sample_size = n())
pal <- c(
"Annelida" = "#D2691E",
"Arthropoda" = "#CDCDB4",
"Chlorophyta" = "#3CB371",
"Chordata" = "#EE9A00",
"Cnidaria" = "#6CA6CD",
"Echinodermata" = "#FF6347",
"Ectoprocta" = "#F4A460",
"Fish" = "#CD3700",
"Heterokontophyta" = "#6B8E23",
"Mollusca" = "#708090",
"Phoronida" = "#FAFAD2",
"Porifera" = "#EEDD82",
"Rhodophyta" = "#DB7093"
)
pal_t <- tibble(group = c(
"Annelida","Arthropoda", "Chlorophyta","Chordata","Cnidaria","Echinodermata","Ectoprocta","Fish","Heterokontophyta","Mollusca","Phoronida","Porifera","Rhodophyta"),
color = c("#D2691E", "#CDCDB4", "#3CB371", "#EE9A00","#6CA6CD", "#FF6347", "#F4A460", "#CD3700", "#6B8E23", "#708090", "#FAFAD2","#EEDD82", "#DB7093"))
library(networkD3)
reef_sankey <- reef_summary %>%
filter(phylum %in% c("Cnidaria", "Mollusca")) %>%
select(phylum, genus, `mean abundance`)
reef_names <- reef_sankey %>%
select(phylum, genus)
node_names <- factor(sort(unique(as.character(unname(unlist(reef_names))))))
nodes <- data.frame(name = node_names)
links <- data.frame(source = match(reef_sankey$phylum, node_names) - 1,
target = match(reef_sankey$genus, node_names) - 1,
value = reef_sankey$`mean abundance`,
group = reef_sankey$phylum)
links <- links[!is.na(links$source), ]
links$group <- reef_sankey$phylum
my_color <- 'd3.scaleOrdinal() .domain(["Annelida","Arthropoda", "Chlorophyta","Chordata","Cnidaria","Echinodermata","Ectoprocta","Fish","Heterokontophyta","Mollusca","Phoronida","Porifera","Rhodophyta", "all_one_group"]) .range(["#D2691E", "#CDCDB4", "#3CB371", "#EE9A00","#6CA6CD", "#FF6347", "#F4A460", "#CD3700", "#6B8E23", "#708090", "#FAFAD2","#EEDD82", "#DB7093", "#1A1FE5"])'
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30,
colourScale = my_color,
LinkGroup="group", NodeGroup = NULL)
```