0

I am trying to make a panel plots of 3 sankey diagram in R using networkD3 library. The Sankey diagrams are well generated and I have assigned three variables (p1, p2 and p3) to the plots. Now I want to combine them using ggarrange into one single plot using the code below;

ggarrange(p1, p2, p3, labels = c("A", "B", "C"), ncol = 1, nrow = 3)

But, I keep getting the following error.

Warning message:
In as_grob.default(plot) :
  Cannot convert object of class sankeyNetworkhtmlwidget into a grob.

Apparently, ggarrange does not accept the format of my plots. How can I convert them into a format that ggarange will accept. Here is the code that I used to generate the final Sankey Diagram for the first one (p1). The rest two (p2, p3) are generated using the same code.

nodes = data.frame("name" = c("Rocket 1", "Kerosene","Carbon IV Oxide", 
                              "Aluminium IV Oxide","Sulphur IV Oxide"))
links = as.data.frame(matrix(c(0, 1, 488.4, 1, 2, 576.7, 1, 3, 24.4,
                               1, 4, 0.3), byrow = TRUE, ncol = 3))
p1 = names(links) = c("source", "target", "value")

p1 = sankeyNetwork(Links = links, Nodes = nodes,
                   Source = "source", Target = "target",
                   Value = "value", NodeID = "name",
                   fontSize= 12, nodeWidth = 20)

I would really appreciate some help with this. Thank you.

user 26
  • 1
  • 1
  • You can't use a `sankeyNetwork` class in `ggarrange`. I would suggest to convert your `sankeyNetwork` to `plotly` and use `subplot`. – Quinten Jun 09 '22 at 16:04
  • Thank you, for suggesting this alternative – user 26 Jun 09 '22 at 22:48
  • {networkD3} creates/exports HTML and JavaScript, it does not create raster or vector images, so it’s fundamentally different than what {ggplot} does and cannot be arranged like that with R’s graphics system. – CJ Yetman Jun 21 '22 at 10:02

0 Answers0