#*****************************************************************************80 # ## web_digraph plots a web of connections as a directed graph. # # Discussion: # # This Rgraphviz package is stoopid. It does not want to label # edges. In this example, I have a directed graph with weighted # edges. I want each edge to be labeled by its weight. But the # software fails in one case, where there are two edges between # nodes Alpha and Epsilon. The software can only use one weight # as a label. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 September 2020 # # Author: # # John Burkardt # library ( Rgraphviz ) cat ( "\n" ) cat ( "web_digraph:\n" ) cat ( " ", version$version.string, "\n" ) cat ( " Plot a web of connections as a directed graph.\n" ) # # Specify the node labels. # nodes <- c ( 'Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta' ) # # Create the graph. # G <- new ( "graphNEL", nodes = nodes, edgemode = "directed" ) # # Add edges. # G <- addEdge ( "Alpha", "Epsilon", G, weight = 0.1 ) G <- addEdge ( "Alpha", "Zeta", G, weight = 0.9 ) G <- addEdge ( "Beta", "Alpha", G, weight = 0.3 ) G <- addEdge ( "Beta", "Gamma", G, weight = 0.4 ) G <- addEdge ( "Beta", "Delta", G, weight = 0.3 ) G <- addEdge ( "Gamma", "Delta", G, weight = 0.6 ) G <- addEdge ( "Gamma", "Epsilon", G, weight = 0.4 ) G <- addEdge ( "Delta", "Epsilon", G, weight = 1.0 ) G <- addEdge ( "Epsilon", "Alpha", G, weight = 0.2 ) G <- addEdge ( "Epsilon", "Zeta", G, weight = 0.8 ) G <- addEdge ( "Zeta", "Delta", G, weight = 1.0 ) # # Idiotic complications to show weights as labels, # and it doesn't even work if a pair of nodes shares two edges. # Why can't I just add a "label = string" argument to addEdge? # ew <- as.character ( unlist ( edgeWeights ( G ) ) ) ew <- ew[setdiff(seq(along=ew), removedEdges(G))] names(ew) <- edgeNames(G) eAttrs <- list() eAttrs$label <- ew attrs <- list() attrs$edge$fontsize <- 27 # # Make the plot. # filename = "web_digraph.png" png ( filename ) plot ( G, recipEdges = "distinct", edgeAttrs = eAttrs, attrs = attrs, main = "web digraph" ) cat ( ' Graphics saved as "', filename, '"\n' ) # # Terminate. # cat ( "\n" ) cat ( "web_digraph:\n" ) cat ( " Normal end of execution.\n" ) quit ( )