rnorouzian
rnorouzian

Reputation: 7517

plot data structure as a tree in R

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

Upvotes: 4

Views: 319

Answers (1)

ASH
ASH

Reputation: 20302

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

Upvotes: 1

Related Questions