4

I'm using sizetree() function from plotrix package to draw my data structure as a tree (see below) and it works just fine.

However, I was wondering if there might be another way (or a package) that would provide a more elegant tree plot of the same data with the same information displayed?

(Note: In the below plot, fonts are unnecessarily either too big or too small so are the rectangles etc. also may be the plot could be inverted to get a better look.)-- it's subjective but I appreciate any suggestion!

library(plotrix)

data <- read.csv('https://raw.githubusercontent.com/hkil/m/master/z.csv')

sizetree(data[c(2,3,5)])

enter image description here

rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • Would a [sankey diagram](https://stackoverflow.com/questions/9968433/sankey-diagrams-in-r) work? – Henrik May 20 '20 at 23:36
  • @Henrik, thank you very much for pointing me to this type of plot. I see many choices there. In my case, things are so simple, though. So I have no idea how my data will show up using a Sankey diagram! – rnorouzian May 20 '20 at 23:47

1 Answers1

1

This is an educated guess. Maybe...

X <- read.csv(url("https://raw.githubusercontent.com/hkil/m/master/z.csv"))

energy <- jsonlite::fromJSON(URL)

# Plot
sankeyNetwork(Links = energy$scid, Nodes = energy$group, Source = 'source',
             Target = 'target', Value = 'value', NodeID = 'name',
             units = 'TWh', fontSize = 12, nodeWidth = 30)

# Colour links
energy$links$energy_type <- sub(' .*', '',
                               energy$nodes[energy$links$source + 1, 'name'])

sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
             Target = 'target', Value = 'value', NodeID = 'name',
             LinkGroup = 'energy_type', NodeGroup = NULL)

enter image description here

See the link below for reference.

https://www.rdocumentation.org/packages/networkD3/versions/0.4/topics/sankeyNetwork

ASH
  • 20,759
  • 19
  • 87
  • 200
  • Thanks! But is this plotting my data?! It doesn't seem so, looking at the labels and the structure of your plot. Also, as I noted in my OP above, I need the same information displayed in my original plot to be displayed. – rnorouzian May 21 '20 at 14:48
  • At the very least, you could use OP's data! –  May 21 '20 at 17:28
  • Well, I used the exact data source that was given listed in the OP. Whatever variables you want to plot is up to you. I use R very infrequently. Maybe 1x in 1 month, or less. I am just trying to give you an idea of what is possible here. I see a few comments, but no other answers, other than mine. – ASH May 21 '20 at 23:58