1

df2 df1

I'm trying to create a Sankey Plot in R, but I keep getting an error that one of the parameters needs to be a Data Frame Class Object. See below:

    library(d3Network)
    d3Sankey(Links = df2[ ,('value')], Nodes = df3[ , c('name')], Source = "source", 
    Target = "target")
Error in d3Sankey(Links = df2[, ("value")], Nodes = df3[, c("name")],  : 
Links must be a data frame class object.
    df2 <- as.data.frame(df2)
    d3Sankey(Links = df2[ ,('value')], Nodes = df3[ , c('name')], Source = "source", 
    Target = "target")
Error in d3Sankey(Links = df2[, ("value")], Nodes = df3[, c("name")],  : 
Links must be a data frame class object.
    typeof(df2)
  [1] "list"

With this information, does anyone know how to convert df2 to a data frame class object?

  • When you use `df2[, "value"]` on a data.frame, it returns a vector. You may need `df2["value"]`. According to documentation for Links and Nodes should be a data.frame i.e. `Links` data.frame should have columns `source`, `target`, and 'value' in a data.frame and `Nodes` with a a column data.frame of 'name' – akrun Mar 12 '22 at 18:31
  • Please provide an example of your data, i.e. `dput(df2)` – deschen Mar 12 '22 at 18:34
  • @akrun Okay I tried that, but now it gives me another error: d3Sankey(Links = df2["value"], Nodes = df1["name"], Source = "source", Target = "target") Error in `[.data.frame`(Links, , Source) : undefined columns selected – Julia Grace Dunn Mar 12 '22 at 18:35
  • @JuliaGraceDunn it is because the `Links` data.frame need 'source', 'target' column as well. i.e. `d3Sankey(Links = df2[c('source', 'target', 'value')], Nodes = df3[c('name')], Source = "source", Target = "target")` – akrun Mar 12 '22 at 18:36
  • @akrun That was super helpful! But when I ran it, I got a lot of words, starting with: "> d3Sankey(Links = df2[c('source', 'target', 'value')], Nodes = df1[c('name')], Source = "source", Target = "target") – Julia Grace Dunn Mar 12 '22 at 18:47
  • YOu need to mention `file` to save as html – akrun Mar 12 '22 at 18:53

1 Answers1

1

The Links, Nodes should be data.frames - Links data.frame should have three columns source, target and value, Nodes with a single column name. In the OP's code, both are provided as vectors because in data.frame, if we use a , with a single column name, it drops the dimensions (drop = TRUE - by default). In order to save the output, specify file

library(d3Network)
d3Sankey(Links = df2[c('source', 'target', 'value')], 
   Nodes = df3[c('name')], Source = "source",   
     Target = "target", file = "sankeyfile.html")
akrun
  • 874,273
  • 37
  • 540
  • 662