Note that there are some explanatory texts on larger screens.

plurals
  1. POPlotting career path in graph
    primarykey
    data
    text
    <p>I have the dataframe</p> <pre><code>test &lt;- structure(list( y2002 = c("freshman","freshman","freshman","sophomore","sophomore","senior"), y2003 = c("freshman","junior","junior","sophomore","sophomore","senior"), y2004 = c("junior","sophomore","sophomore","senior","senior",NA), y2005 = c("senior","senior","senior",NA, NA, NA)), .Names = c("2002","2003","2004","2005"), row.names = c(c(1:6)), class = "data.frame") &gt; test 2002 2003 2004 2005 1 freshman freshman junior senior 2 freshman junior sophomore senior 3 freshman junior sophomore senior 4 sophomore sophomore senior &lt;NA&gt; 5 sophomore sophomore senior &lt;NA&gt; 6 senior senior &lt;NA&gt; &lt;NA&gt; </code></pre> <p>and I want to create a graph that should resemble the ugly text art below:</p> <pre><code>freshman ---- junior ----------------------\ freshman ---- junior --- sophomore -------- senior sophomore ================================/ senior ---------------------------------/ </code></pre> <p>In other words, I need to show in a graph the possible paths to "senior", giving weights to edges according to the number of cases using that path.</p> <p><strong>First attempt</strong> This code generates a graph, <em>but not one similar to the text art above</em>.</p> <pre><code>library(igraph) elist &lt;- lapply(seq_len(nrow(test)), function(i) { x &lt;- as.character(test[i,]) x &lt;- unique(na.omit(x)) x &lt;- rep(x, each=2) x &lt;- x[-1] x &lt;- x[-length(x)] r &lt;- matrix(x, ncol=2, byrow=TRUE) if (nrow(r) &gt; 0) { r &lt;- cbind(r, i) } else { r &lt;- cbind(r, numeric()) } r }) result &lt;- as.data.frame(do.call(rbind, elist)) names(result) &lt;- c("vertex","edge", "id") categories &lt;- data.frame(name=c("freshman","junior","sophomore","senior")) g &lt;- graph.data.frame(result,directed=T,vertices=categories) g &lt;- set.edge.attribute(g, "weight", value=runif(ecount(g))*10) igraph.par("plot.layout", layout.reingold.tilford) plot(g, vertex.label=categories$name, vertex.label.dist=7, edge.width=get.edge.attribute(g,"weight"), edge.arrow.size=1.5) </code></pre> <p><strong>Result</strong> (not what I wanted)</p> <p><img src="https://i.stack.imgur.com/H1Gqp.png" alt="resulting image"></p> <p>**This question is related to <a href="https://stackoverflow.com/questions/12362866/from-dataframe-to-vertex-edge-array/12382276#12382276">this post</a>*</p> <p>**And <a href="https://stackoverflow.com/questions/12417467/reduce-row-to-unique-items/12418321">this post</a> is a necessary step for solving this question*</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload