0

I'm trying to add legend to sankey network plot using NetworkD3 library and as NetworkD3 does not have a legend option, I'm trying to create a legend by using legend() function however its not working.

This is what I tried, but it gives only the Sankey as output.

graphics.off()
par("mar")
par(mar=c(1,1,1,1))
par(mfrow = c(2,1))

sankey_plot <- sankeyNetwork(sinksRight = FALSE, Links = links, Nodes = Nodes, 
                               Source = 'source', Target = 'target', Value = 'value', 
                               NodeID = 'name', LinkGroup = 'group', NodeGroup = 'group',
                               fontSize = 8.6, height = "720px", width = "1080px")
sankey_plot


plot(NULL ,xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=0:1, ylim=0:1)

legend("topleft", legend =c('Sugar maple', 'White ash', 'Black walnut','Red oak', 'Eastern hemlock'), pch=16, pt.cex=3, cex=1.5, bty='n', col = c('orange', 'red', 'green', 'blue', 'purple'))
   
mtext("Species", at=0.2, cex=2) 
user438383
  • 5,716
  • 8
  • 28
  • 43
sammy
  • 11
  • 2

0 Answers0