score:0

as already mentioned in the comments, you might want to give networkd3 a shot.

here is an example based on the sample data you provide.

# create nodes and links data.frames
nodes <- data.frame(name = unique(unlist(hc_dat[, 1:2])))
links <- data.frame(
    source = match(hc_dat$from, nodes$name) - 1,
    target = match(hc_dat$to, nodes$name) - 1,
    value = hc_dat$n)

# draw a sankey diagram
library(networkd3)
sankeynetwork(
    links = links, nodes = nodes,
    source = "source", target = "target", value = "value", nodeid = "name",
    fontsize = 16, fontfamily = "sans-serif", nodewidth = 30, nodepadding = 30)

enter image description here

score:3

sankey from a->b and from b->c can be done by re-defining your underlying data:

 hc_dat <- data.frame(from = c("a", "b"), 
                 to = c("b", "c"), n = c(7, 5))

similarly, you can define node from a->c

 hc_dat <- data.frame(from = c("a", "b", "a"), 
                 to = c("b", "c", "c"), n = c(5, 5, 7))

this does not render a nice plot, though.


Related Query

More Query from same tag