A61 Tree Diagram - cimat/data-visualization-patterns GitHub Wiki

A 6.1: TREE DIAGRAM

Description

A tree diagram is a graphic representation of a strictly hierarchical structure, which describes a set of elements and their relations to each other. All elements of such a structure have family like relations to other elements, qualifying them on an higher, lower or equal level in the set’s hierarchy. One particular element (the root element) does not have any superior or equal elements(Behrens, 2008).

Required Data

Use tree diagrams to display data that consist of several items which are ordered in a hierarchy structure one item acts as the “root”, while the other items relate to it as subordinated “family members” (children, grandchildren etc.) (Behrens, 2008).

Usage

Identify the root member of the dataset. Draw the graphic object its represents, and attach its child elements on the appropriate visual level, for example one step below. Connect directly related elements with each other through a stroke. Continue accordingly with the remaining elements of the dataset. Maintain visually distinct “family levels”, for instance by placing children always below their parents, and siblings on an equal height, respectively with equal visual distance between equal distances of generations (Behrens, 2008).

Rationale

The tree is the commonly accepted way of displaying family-style structures; constructs of elements that stand in some kind of inheritance relationship to each other. Through the strict placement of different generations on different levels the user can easily identify causal relationships within the data structure.

Especially in connection to computers, tree structures are a somewhat ubiquitous concept of organizing data; an operating system maintains files in a tree, the functions of many applications are arranged hierarchically, just as the content of complex editors. The majority of websites provides its content in a similar way, so that users of digital media are sort of anticipating information offered in a tree-like fashion: Choosing one of a few main branches in the beginning, the search becomes more detailed the deeper he drills down the tree (Behrens, 2008).

Related Patterns

  • A 5.2 Thread Arcs
  • A 6.2 Tree Map

PYTHON IMPLEMENTATION

Data Set

For this proyect it will use the dataset mtcars of R.

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models)

from datos import data
d=data('mtcars')

Dependences

  • rpy2 Python interface to the R language (Gautier, 2016)[^1].
  • Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and six graphical user interface toolkits (Hunter, 2016)[^2].
  • Graphviz Simple Python interface for Graphviz. This package facilitates the creation and rendering of graph descriptions in the DOT language of the Graphviz graph drawing software from Python (Bank, 2016)[^3].

Code Example

Matplotlib

import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
from matplotlib.collections import PatchCollection
from datos import data

fig = plt.figure()
ax = fig.add_subplot(111, frameon=False)
d=data('mtcars')
subset1= d[d.cyl==6]
patches = []
arrow = mpatches.Arrow(0.492,0.9, 0, -0.1, width=0.05)
patches.append(arrow)
rect=mpatches.Rectangle((0.35, 0.9), 0.3, 0.1)
fig.text(0.415,0.85,"Car Distribution by Cylindres", fontsize='8')
patches.append(rect)
arrow = mpatches.Arrow(0.492,0.7, 0.0, -0.045, width=0.05)
patches.append(arrow)
rect=mpatches.Rectangle((0.35, 0.7), 0.3, 0.1)
fig.text(0.47,0.7,"6 Cylindres", fontsize='8')
patches.append(rect)
x=0.0
x1=0.13
y=0.45
y1=0.49
xx=0.06
for i in subset1.index:
    arrow = mpatches.Arrow(xx,0.65, 0.0, -0.1, width=0.05)
    patches.append(arrow)
    if len(i)>12:
        fig.text(x1,y1,i[0:9]+"\n"+i[9:], fontsize='8')
    else:
        fig.text(x1,y1,str(i), fontsize='8')
    rect=mpatches.Rectangle((x, y),0.125, 0.1)
    patches.append(rect)
    x=x+0.145
    x1=x1+0.113
    xx=xx+0.145

x, y = np.array([[0.06, 0.93], [0.65, 0.65]])
line = mlines.Line2D(x , y , lw=5., alpha=0.3)
ax.add_line(line)
colors=[0,1]
p = PatchCollection(patches,  alpha=0.3)
p.set_array(np.array(colors))
ax.add_collection(p)
plt.title('Tree Diagram ', family='serif', size=16)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
plt.show()

The complete online documentation is also available at matplotlib

Graphviz

from graphviz import Digraph
from datos import data

d=data('mtcars')
subset1= d[d.cyl==6]
dot = Digraph(comment='Tree Diagram', format='png')
dot.attr('node', shape='box')
dot.node_attr.update(color='lightblue2', style='filled')
dot.node('A', 'Car Distribution by Cylindres')
dot.node('B', '6 cylindres')
j=67;
for i in subset1.index:
        dot.node(unichr(j), str(i))
        print i+"\n"
        j=j+1

dot.edge('A', 'B')
dot.edges(['BC','BD','BE','BF','BG','BH','BI'])
dot.body.append(r'label = "\n\nTree Diagram"')
dot.body.append('fontsize=20')
dot.render('diagram')

The link for Documentation is Graphviz

References

[^1]: Gautier, Laurent (2016). rpy2. Consultado el 09 de Febrero, 2016 en http://rpy2.bitbucket.org/ [^2]: Hunter, John (2016). matplotlib. Consultado el 13 de Febrero, 2016 en http://matplotlib.org/ [^3]: Bank, Sebastian (2016). graphviz. Consultado el 07 de Mayo, 2016 en http://graphviz.readthedocs.io/en/latest/

R IMPLEMENTATION

For implementation of pattern Tree Diagram in R, it use the package "diagram". This package include functions for visualises simple graphs (networks) based on a transition matrix, utilities to plot flow diagrams, visualising webs, electrical networks, ... (Soetaert, 2014)[1].

Data Set

For this proyect it will use the dataset mtcars.

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

head(mtcars)

##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Dependences

  • diagram

Code Example

Diagram

library(diagram)

## Loading required package: shape

par(mfrow=c(1,1))
par(mar=c(0,0,0,0))
openplotmat(main="Tree Diagram ")
x<-subset(mtcars, mtcars$cyl==4)
y<-subset(mtcars, mtcars$cyl==6)
z<-subset(mtcars, mtcars$cyl==8)
elpos<-coordinates (c(1,1,7))
treearrow(from=elpos[1,],to=elpos[2,],lwd=4)  
treearrow(from=elpos[2,],to=elpos[3:9,],lwd=4) 
labs<-c("Car Distribution \n by Cylindres", "6 Cylindres",  row.names(y))
for ( i in 1:length(labs)) textrect(elpos[i,],radx=0.06,rady=0.05,lab=labs[i] ,cex=0.6, col = "blue", box.col="grey")

The complete online documentation is also available in the form of a single PDF file at CRAN.

From within R, type: > help(diagram)

References

[1] Soetaert, Karline (2014). CRAN. Consultado el 12 de Mayo, 2016 en https://cran.r-project.org/web/packages/diagram/index.html

⚠️ **GitHub.com Fallback** ⚠️