# The MIT License
# Copyright (c) 2007 The GGobi Foundation
# http://www.ggobi.org/book/code-license.txt

# To run this code, you need to have the following software
# on your machine:
#
#  R                  r-project.org
#  ggobi              ggobi.org
#  graphviz           graphviz.org
#  rggobi             ggobi.org
#  graph, SNAData     bioconductor.org

library(rggobi)
library(graph)
library(SNAData)

data(florentineAttrs, business, marital)

# Node data: families
families = florentineAttrs
ties = families[,"NumberTies"]

gg = ggobi(families)

# Edges: business relationships
# Add edge names
e = t(edgeMatrix(business))
src = nodes(business)[e[,1]]
dest = nodes(business)[e[,2]]
edgenames = paste(src, dest, sep="->")
# Add edge weights: the average of the two node variables
weights = matrix((ties[e[,1]] + ties[e[,2]]) / 2, ncol=1)
dimnames(weights) = list(edgenames, c("meanTies"))

gg$business = data.frame(weights)
edges(gg$business) = cbind(src, dest)

# Edges: marital relationships
# Add edge names
e = t(edgeMatrix(marital))
src = nodes(marital)[e[,1]]
dest = nodes(marital)[e[,2]]
edgenames = paste(src, dest, sep="->")
# Add edge weights: the average of the two node variables
weights = matrix((ties[e[,1]] + ties[e[,2]]) / 2, ncol=1)
dimnames(weights) = list(edgenames, c("meanTies"))

gg$marital = data.frame(weights)
edges(gg$marital) = cbind(src, dest)

# You'll see a scatterplot of 'Number of Ties' against 'Wealth'
# for each of the families.  Use the Tools menu in ggobi to open
# the GraphLayout plugin.  Perform a neato layout with 'business'
# as the edge set, and again with 'marital' as the edge set.
# Use Identification in the scatterplot to look at the ties of the
# wealthiest families.


