Title: Easily print a nicely looking tree diagram from data in R
Tags: r
Question: I have a dataset like the following (this will be of arbitrary length, but with one count column at the end):
dd <- data.frame(
"level1" = c("a", "a", "b"),
"level2" = c("c", "d", "c"),
"cnt" = c(1, 3, 5)
And I'd like to print out a nice looking tree of this structure, without a ton of work :).  Already attempted just doing a sort, and looking at boundary conditions to output data.
Ideally the tree structure will look like this from the data above:
level1     level2
a: 4  ---&gt; c: 1
---&gt; d: 3
b: 5  ---&gt; c: 5
Where the last vector of the dataframe gets summed up the distinct branches of the tree, but with nice diagrams for visualization.  Last vector, because we can have an arbitrary number of levels.  Does anyone know an easy path forward for this without writing out my own set of tree algorithms?
Image of graph:
Comment: How come two branches (a and b) end up in the same node (c)?

Comment: Can you explain why you have the `b` in level two? Did you mean `d` instead?

Comment: Also, do you always have only two columns with arbitrary number of rows? Or can you have arbitrary number of levels (columns) as well?

Comment: Where did the numbers 4 and 5 (on the left side) come from?

Comment: Good Q's asb.  Arbitrary number of levels.

Comment: Its a product of the way the data is generated, its a real process that can do that, and useful for viewers to see it that way.

Comment: Sure.  that's fair.  Done @agstudy.

Comment: Sum up the counts associated with the children.


Here is another answer: Using igraph package for example, you can get with little work. I corrected some typo in your data and add a new edge to get prettier graph. My graph look like this one:
And here my R code:
actors <- data.frame(name=c('a','b','c','d'))
relations <- data.frame(from=c("a", "a", "b",'c'),
to=c("c", "b", "c",'d'),
weight=c(1, 3, 5,4))
g <- graph.data.frame(relations, directed=TRUE,
vertices=actors)
E(g)$label=E(g)$weight
E(g)$label.cex=3
plot(g,edge.width=E(g)$weight,layout=layout.fruchterman.reingold)
Comment for this answer: @mcpeterson relations is your DD but I suppose here that you have a graph not a tree.
Comment for this answer: I should have specified more clearly, but I need to solve this for the arbitrary case, and its not clear to me how to fill in your actor and relations frames from an arbitrary data set of the shape I provided.
In other words, how would you work from DD to generate your graph?
Also, it really needs to be a tree from one level to the next.  No values in "level2" of dd will ever be pointed to jointly by two values from "level1"

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:06/27/13', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<r>', 'Title': 'Easily print a nicely looking tree diagram from data in R', 'ViewCount': 503, 'id': 17332592, 'user_AboutMe': '', 'user_AccountId': 59045.0, 'user_DisplayName': 'mcpeterson', 'user_Reputation': 4694.0, 'user_Views': 272.0, 'user_id': 176696.0}}