Skip to content

Instantly share code, notes, and snippets.

@abresler
Last active December 18, 2015 15:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abresler/c1a51cdbc475119bd492 to your computer and use it in GitHub Desktop.
Save abresler/c1a51cdbc475119bd492 to your computer and use it in GitHub Desktop.
# Reading in 'dogs.csv'
# Created by Alex Bresler, available at:
# https://github.com/abresler/abresler.github.io/blob/master/blog/2015/february/exploring_agility_show/data/dogs.csv
dogs <- read.csv("dogs.csv", header = TRUE, stringsAsFactors = FALSE)
# Setting a numeric id
node_id <- 1:nrow(dogs)
# Getting unique values for the 'breed' column
unique_node_2 <- unique(dogs$breed)
# Getting secondary node id values from arbitrary column (breed in this case)
node_id_2 <- vector(mode = "numeric")
for (i in 1:nrow(dogs)){
node_id_2 <- c(node_id_2, which(unique_node_2 %in% dogs$breed[i]))
}
top_five_id_2 <- sort(table(node_id_2), decreasing = TRUE)[1:5]
# Setting the maximum height of a scaled node
maxheight <- 0.5
# Scaling the maximum height of the node
height <- dogs$height_class/max(dogs$height_class) * maxheight
# Adding new columns to the 'dogs' data frame, because 'height' is included
# it'll be applied as a parameter to each node staement
dogs <- cbind(node_id, height, dogs, node_id_2)
# Creating node statements for the primary nodes (all dogs)
node_statements_1 <- graphviz_node_edge_blocks_df(nodes = dogs)
# Creating node statements for the secondary nodes (all breeds of dogs)
node_statements_2 <-
paste(paste0(" node [peripheries = 2",
ifelse(unique(node_id_2) %in% as.numeric(names(top_five_id_2)),
paste0(", label = '", unique_node_2[unique(node_id_2)], "']"), "]"),
"'2_", unique(node_id_2), "' \n"), collapse = "; ")
# Creating edge statements (from each dog to the breed of dog)
edge_statements_1 <- paste(gsub("\\+", "",
paste0("'", "1_", dogs$node_id, "' -> '",
"2_", dogs$node_id_2, "'")),
collapse = "\n")
# Let's make a 'twopi' styled graph
grViz("
digraph dogs {
# Graph statements
graph [layout = twopi, overlap = false]
# Node statements
node [shape = circle, label = '', style = filled, color = lightblue]
@@1
node [height = 1, label = '', style = filled, color = turquoise]
@@2
# Edge statements
edge [color = gray, arrowsize = 0.5]
@@3
}
[1]: node_statements_1
[2]: node_statements_2
[3]: edge_statements_1
")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment