Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Diagramme R graph in r-stats: node spacing and other formatting in tree

$
0
0

I'm building a conceptual model that involves:

  1. Activities
  2. Direct Effects
  3. Indirect Effects
  4. Valued Components

The activities link to direct and indirect effects that link to the valued components. The edges create a lot of traffic, so I'm working carefully on the style and format to clearly show the relationships.

I initially coded a graph using grViz, but could not find a way to get the activities to point to Direct Effects on one side and Indirect Effects on the other side using duplicated columns of the VC's on both sides that the effects lead into. The key problem was duplicating the VC nodes as illustrated here:

Concept Model - graph

The code I used:

library(DiagrammeR)

####
## Construction Phase ##
####

grViz("
  digraph dot {

graph [ layout = dot,
        overlap = true,
        fontsize = 10,
        rankdir = LR,
        nodesep = 0.2]

#######
## Setting the 'node' statements
#######

  ## Activities
      node [shape = box,
        style = filled,
        color = SandyBrown,
        fixedsize = true,
        fontname = Helvetica,
        penwidth = 1.0]
  # See Excel for Activity Codes
  'A1'; 'A2'; 'A3'; 'A4'; 'A5'; 'A11'; 'A17'; 'A18'; 'A21'; 'A24'

  ## Indirect Effects
      node [shape = polygon,
        sides = 6,
        fixedsize = true,
        style = filled,
        color = Wheat,
        width = 2.0]
      'Fragmentation'; 'Deterioration'; 'Contamination '

  ## Direct Effects
      node [shape = polygon,
        fixedsize = true,
        width = 2.0,
        style = filled,
        color = Tan]
      'Mortality'; 'Sensory'; 'Contamination'

  ## VC's
      node [shape = circle,
        fixedsize = true,
        style = filled,
        color = PaleGoldenrod,
        width = 0.6
        ]
        'AMMA'; #Long-toed Salamander
        'ANBO'; #Western Toad
        'TATA'; #American Badger
        'MAAM'; #American Marten
        'MBCA'; #Birds protected by MBCA and WA
        'CEEL'; #Elk
        'URAR'; #Grizzly Bear
        'MYLU'; #Little Brown Myotis
        'ALAL'; #Moose
        'NOGO'; #Northern Goshawk
        'SEOW'; #Short-Eared Owl
        'SWHA' #Swainson's Hawk

#####
## Edge statements - pathways
#####

  ## Indirect Effects
'Fragmentation' -> {'A2''A21''A11''A3'}[arrowhead = inv, dir = back, color=Grey]

'Deterioration' -> {'A5''A1''A11''A17''A18''A2''A21''A3''A4'}[arrowhead = inv, dir = back, color=Grey]

'Contamination ' -> 'A5'[arrowhead = inv, dir = back, color=Grey]


  ## Direct Effects on VC's
  'Mortality' -> {'AMMA''ANBO''TATA'
                  }[color=red, style=dashed, arrowhead = tee, arrowtail = none]

  'Sensory' -> {'TATA''MAAM''MBCA''CEEL''URAR''MYLU''ALAL''NOGO''SEOW''SWHA'
               }[color=blue, style=dashed, arrowhead = tee, arrowtail = none]

  'Contamination' -> {'TATA''MBCA''CEEL''URAR''MYLU''AMMA''ALAL''SEOW''SWHA''ANBO'
                    }[color=purple, style=dashed, arrowhead = tee, arrowtail = none]

  ## Activity to Effect
  'A1' -> 'Mortality'[color=red]
  'A1' -> 'Sensory'[color=blue]
  'A2' -> 'Mortality'[color=red]
  'A2' -> 'Sensory'[color=blue]
  'A3' -> 'Mortality'[color=red]
  'A3' -> 'Sensory'[color=blue]
  'A4' -> 'Mortality'[color=red]
  'A4' -> 'Sensory'[color=blue]
  'A5' -> 'Mortality'[color=red]
  'A5' -> 'Sensory'[color=blue]
  'A5' -> 'Contamination'[color=purple]
  'A11' -> 'Mortality'[color=red]
  'A11' -> 'Sensory'[color=blue]
  'A17' -> 'Mortality'[color=red]
  'A17' -> 'Sensory'[color=blue]
  'A18' -> 'Mortality'[color=red]
  'A18' -> 'Sensory'[color=blue]
  'A21' -> 'Mortality'[color=red]
  'A21' -> 'Sensory'[color=blue]
  'A24' -> 'Mortality'[color=red]
}
")

I turned to DiagrammeR to put nodes and edges into dataframes like so:

   library(DiagrammeR)

# Create a Node Data Frame (NDF) for Activities
Act_nodes <- create_node_df(
  n = 10,
  type = "a",
  label = c("A1", "A2", "A3", "A4", "A5", "A11", "A17", "A18", "A21", "A24"),
  data = 1:10,
  shape = "box",
  fixedsize = TRUE,
  fontname = "Helvetica",
  penwidth = 1.0,
  width = 0.5,
  style = "filled",
  color = "black",
  fillcolor = "SandyBrown",
  fontcolor = "black")

DE_nodes <- create_node_df(
  n = 3,
  type = "b",
  label = c("Mortality", "Sensory", "Contamination"),
  data = 11:13,
  shape = "polygon",
  sides = 6,
  color = "black",
  fillcolor = "tan",
  fontcolor = "black",
  fixedsize = TRUE,
  fontname = "Helvetica",
  penwidth = 1.0,
  width = 1.0,
  style = "filled",
  x = c(-80,80,-80),
  y = c(-80,80,80))

Act_DE_comb <- combine_ndfs(
  Act_nodes,
  DE_nodes)

## Creating the edge data frames for Indirect Effects
 edges_contaminate <- 
      create_edge_df(
        from = c(5),
        to = c(13),
        color = "purple",
        rel ="a", 
        length = 500)

    edges_mortality <- 
      create_edge_df(
        from = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
        to = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11),
        color = "red",
        rel ="a")

    edges_sensory <- 
      create_edge_df(
        from = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
        to = c(12, 12, 12, 12, 12, 12, 12, 12, 12),
        color = "blue",
        rel ="a")

    # Combine the edge data frames
    all_edges <-
      combine_edfs(edges_contaminate, edges_mortality, edges_sensory)

    graph <- create_graph(
        nodes_df = Act_DE_comb,
        edges_df = all_edges)

    render_graph(graph, layout = "tree")

This produced the following:

DiagrammeR Tree graph of activities to direct effects

My plan is to combine graphs:

  1. Activities to Direct Effects
  2. Direct Effects to Valued Components
  3. Activities to Indirect Effects
  4. Indirect Effects to Valued Components

I'm hoping that I can duplicate the Valued Component nodes to appear on a left and right side as I illustrated above. However, the problem I am facing is the spacing between the direct effect nodes. In the grViz version the nodes had sufficient separation. I followed this question and answer post: How to increase distance between nodes in DiagrammeR R

I've tried to insert x, y coordinates and it shows up in the dataframe (get_node_attrs):

  id type         label data   shape fixedsize  fontname penwidth width
1   1    a            A1    1     box      TRUE Helvetica        1   0.5
...
10 10    a           A24   10     box      TRUE Helvetica        1   0.5
...
13 13    b Contamination   13 polygon      TRUE Helvetica        1   1.0

    style color  fillcolor fontcolor sides  x  y
1  filled black SandyBrown     black    NA NA NA
...
10 filled black SandyBrown     black    NA NA NA
...
13 filled black        tan     black     6 18 43

I also used nudge_node_positions_ws, but the nodes do not move. I believe this has to do with layout = "tree" that does not allow the nodes to move. How can I get the nodes to move? I'm also thinking that I could pass the dataframes into digraph or Graphviz (e.g., http://www.graphviz.org/pdf/dotguide.pdf), but I don't know if that is necessary or how to do this yet. Is there a way to move the nodes using DigrammeR in tree layout and duplicate a group of nodes to appear on left and right sides?

Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>