SPSS Statistics

View Only

Network Xmas Tree in SPSS

By Archive User posted Thu December 19, 2013 09:00 AM

Motivated by Rick Wicklin's raster based Christmas Tree in SAS, here I will show how to lay out a network Xmas tree in SPSS - XKCD network style.

SPSS's GPL language has the ability to lay out different network diagrams given a list of edges and vertices. One of the layouts is a tree layout, and is intended for hierarchical data. Dendrograms created from hierarchical clustering are some of the most popular uses for this type of layout.

So to create the data for our Xmas tree, what I first did was just drew the XKCD tree on a piece of paper, and then replaced the ornaments with an integer value used to represent a node. Below is my best ASCII art representation of that tree.
``              1          ____|______         |           |         2           3     ____|      _____|_____    |          |           |    4          5           6 ___|____      |       ____|____|        |     |      |         |7        8     9     10        11|_____      ___|___   |         |         |     |    |       |  |         |12   13    14      15 16       17``

From here we can make an edge dataset that consists of the form of all the directed connections in the above graph. So `1` is connected to `2`, `2` is connected to `4` etc. That dataset will look like below.
``data list free / XFrom XTo.begin data1 21 32 43 53 64 74 85 96 106 117 127 139 149 1510 1611 17end data.dataset name edges.``

If all I wanted to do was to draw the edges this would be sufficient for the graph. But I also want to style the nodes, so I create a dataset listing the nodes and a color which I will draw them with.
``data list free / Node (F2.0) Type (A15).begin data1 Yellow2 Red3 Red4 Green5 Red6 Red7 Red8 Red9 Red10 Green11 Green12 Green13 Red14 Green15 Red16 Green17 Redend data.dataset name nodes.``

Now here is the magic of GPL to draw our Xmas tree.
``GGRAPH  /GRAPHDATASET NAME="edges" DATASET = "edges" VARIABLES=XFrom XTo  /GRAPHDATASET NAME="nodes" DATASET = "nodes" VARIABLES=Node Type  /GRAPHSPEC SOURCE=INLINE.BEGIN GPL SOURCE: e=userSource(id("edges")) DATA: XFrom=col(source(e), name("XFrom"), unit.category()) DATA: XTo=col(source(e), name("XTo"), unit.category()) SOURCE: n=userSource(id("nodes")) DATA: Node=col(source(n), name("Node"), unit.category()) DATA: Type=col(source(n), name("Type"), unit.category()) GUIDE: axis(dim(1), null()) GUIDE: legend(aesthetic(aesthetic.color.interior), null()) COORD: rect(dim(1,2), reflect(dim(2))) SCALE: cat(aesthetic(aesthetic.color.interior), map(("Yellow", color.yellow), ("Red", color.red), ("Green", color.green))) ELEMENT: edge(position(layout.tree(node(Node),from(XFrom), to(XTo), root("1")))) ELEMENT: point(position(layout.tree(node(Node),from(XFrom), to(XTo), root("1"))), color.interior(Type), size(size."14"))END GPL.``

I ended up drawing the Y axis (and reflecting it) because my chart template did not have enough padding for the root node and the yellow circle was partially cut off in the plot. I then post-hoc deleted the Y axis, changed the aspect ratio and the background color of the plot. And Voila - Happy Holidays!

#datavisualization
#grammarofgraphics
#network
#SPSS
#SPSSStatistics
#Visualization
1 comment
2 views