Draw Sankitu with R (flowchart)

Source: Internet
Author: User
Sankey diagrams can be used to represent transitions between individual nodes

In R, you can directly define the relationship between points and use the corresponding package to draw the Sankey diagram:


method One:

Riverplot

Library (Riverplot) # The data frame that constructs the connection node edges = data.frame (N1 = Paste0 (Rep (letters[1:4], each = 4), Rep (1:5, each = 16)),  N2 = Paste0 (Rep (Letters[1:4], 4), Rep (2:6, each =), Value = runif (min = 2, max = 5) * Rep (c (1, 0.8, 0.6, 0.4, 0.3), each = All), stringsasfactors = F) # filter 80% records so that each point corresponds to 4 points edges = Edge S[sample (c (TRUE, FALSE), nrow (edges), replace = TRUE, prob = C (0.8, 0.2)),] head (edges) nodes = data.frame (ID = unique (c (Edges$n1, EDGES$N2)), stringsasfactors = FALSE) # nodes$x = As.integer (substr (Nodes$id, 2, 2)) Nodes$y = As.integer (sappl Y (substr (nodes$id, 1, 1), Chartoraw))-X # rownames (nodes) = Nodes$id Head (nodes) # Add color library (rcolorbrewer) # After adding light color palette = PASTE0 (Brewer.pal (4, "Set1"), "60") # Generate corresponding format for each node styles = lapply (nodes$y, function (n) {list (col = palet Te[n+1], lty = 0, Textcol = "Black")}) names (styles) = Nodes$id # In the structure of the list to save the call to the RP <-list (nodes = nodes, edges = E Dges, styles = styles) CLass (RP) <-C (Class (RP), "Riverplot") plot (RP, Plot_area = 0.95, yscale=0.06) 

See the effect diagram:



Method Two:

Using the package d3network or circlize,d3network is called D3 's paint function, using the code to implement:

Library (d3network)
d3links <-edges
d3nodes <-data.frame (name = Unique (c (edges$n1, edges$n2)), Stringsasfactors = FALSE)
d3nodes$seq <-0: (Nrow (D3nodes)-1)

d3links <-merge (D3links, D3nodes, by.x= "N1 ", by.y=" name ")
names (D3links) [4] <-" source "

d3links <-Merge (D3links, D3nodes, by.x=" N2 ", by.y=" name ")
names (D3links) [5] <-"target"
names (D3links) [3] <-"value"

d3links <-subset (D3links, Select=c ( "Source", "target", "value"))
d3nodes <-subset (D3nodes, Select=c ("name")

# Draw and Save as HTML file
D3sankey (Links = d3links, Nodes = d3nodes, Source = "source",
         target = "target", value = "value", NodeID = "name",
         fontsize = A, Nodewidth = +, File = "testsankey.html")


Effect Diagram:



remark:

(1) D3network saved as HTML file with interactive effect; Riverplot can be saved as a different picture type

(2) feel easy to adjust with Riverplot in operation


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.