|
a |
|
b/RNA-seq/Functions/Plot.Sankey.R |
|
|
1 |
#'@param data: data.frame, store three columns |
|
|
2 |
#ALX3 CYB5A 1 |
|
|
3 |
#HESX1 CYB5A 1 |
|
|
4 |
#MYF6 CYB5A 1 |
|
|
5 |
#VSX1 CYB5A 1 |
|
|
6 |
#'@param LinkGroup: character, indicates the variable used for link classification, the default is target |
|
|
7 |
|
|
|
8 |
Plot.Sankey <- function(data, saveDir, filename = "Sankey", LinkGroup = "target"){ |
|
|
9 |
require(tidyverse) |
|
|
10 |
require(viridis) |
|
|
11 |
require(patchwork) |
|
|
12 |
require(networkD3) |
|
|
13 |
require(RColorBrewer) |
|
|
14 |
|
|
|
15 |
#Read the data frame just now and name the column |
|
|
16 |
colnames(data) <- c("source","target","value") |
|
|
17 |
|
|
|
18 |
#All nodes in the graph |
|
|
19 |
data.nodes <- data.frame(name=c(as.character(data$source), as.character(data$target)) %>% unique()) |
|
|
20 |
|
|
|
21 |
#Renumber the source and target |
|
|
22 |
data$IDsource <- match(data$source, data.nodes$name)-1 |
|
|
23 |
data$IDtarget <- match(data$target, data.nodes$name)-1 |
|
|
24 |
|
|
|
25 |
#This step is to customize the color for flow, and define a column separately |
|
|
26 |
data$group="flow" |
|
|
27 |
|
|
|
28 |
#Configure the color part, the form is relatively fixed, no need to entangle specific grammatical rules |
|
|
29 |
#The color corresponding to the code can be viewed with the RColorBrewer package combined with the scales package |
|
|
30 |
my_color <-'d3.scaleOrdinal() |
|
|
31 |
.domain(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J","flow"]) |
|
|
32 |
.range(["#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3", "#FDB462", "#B3DE69", "#FCCDE5", "#BC80BD", " #CCEBC5","#BDBDBD"])' |
|
|
33 |
|
|
|
34 |
#Drawing |
|
|
35 |
Sankey.p <- sankeyNetwork(Links = data, Nodes = data.nodes, |
|
|
36 |
Source = "IDsource", Target = "IDtarget", |
|
|
37 |
Value = "value", NodeID = "name", LinkGroup = LinkGroup,colourScale = my_color, |
|
|
38 |
sinksRight=FALSE, nodeWidth=25, nodePadding=10, fontSize=13,width=900) |
|
|
39 |
# The main parameters: |
|
|
40 |
# Links refers to a data frame, including source, target, and value columns. Where source and target use the code replacement or directly correspond to the name. |
|
|
41 |
# Nodes refers to the names of all points, you can get the names in the links or correspond to the codes in the links yourself. |
|
|
42 |
# Source, target, value corresponds to the value in the links. |
|
|
43 |
# NodeID corresponds to the name in Nodes. Here, if the corresponding ID is required, the nodes in links need to be numbered starting from 0. |
|
|
44 |
# NodeGroup, LinkGroup refers to the changes in the colors of the corresponding nodes and connecting lines. If they are grouped, the colors between different groups will be marked differently. |
|
|
45 |
# Nodewidth refers to the width of the node. |
|
|
46 |
#Draw the graphics, you can also directly adjust the upper and lower positions of the square in the R window, but it is still recommended to save it as pdata and use AI to adjust |
|
|
47 |
#nodeWidth The width of each node block |
|
|
48 |
#nodePadding The vertical interval between each column of node squares |
|
|
49 |
#width is the horizontal width of the graphic |
|
|
50 |
library(htmlwidgets) |
|
|
51 |
saveWidget(Sankey.p, file = file.path(saveDir, paste0(filename, ".html"))) |
|
|
52 |
library(webshot) |
|
|
53 |
webshot(file.path(saveDir, paste0(filename, ".html")), file.path(saveDir, paste0(filename, ".pdf"))) |
|
|
54 |
return(NULL) |
|
|
55 |
} |