Deividas Kiznis Deividas Kiznis - 6 months ago 45
R Question

How to create tree graph in R

I was wondering it is possible to create something like tree diagram using imported data table (utf-8 coded) similar to this:

col0 col1 col2 col3 col4
name <NA> <NA> <NA> NA
name dog, cat, bird dog Canis 3
name dog, cat, bird cat Feline 3
name dog, cat, bird bird Avis 4

I try use this code sample:

raw = subset(dataset, select=c("col1","col2","col3"))
formula = col1 ~ col3
fit = rpart(formula, method="class", data=raw)

But I get error like: "number of rows of matrices must match" or in other cases this pop up: "Error: unexpected '>' in ”>"

In the end I'm trying get diagram that are more or less like a picture below:enter image description here


You could use the package diagrammer to give you an image like this:

enter image description here

dataset <- data.frame(
  col1=c(NA, "dog, cat, bird", "dog, cat, bird", "dog, cat, bird"),
  col2=c(NA, "dog", "cat", "bird"),
  col3=c(NA, "Canis", "Feline", "Avis"),
  col4=c(NA, 3, 3, 4),

# remove rows with missing values
sub <- dataset[!$col1), ]

uniquenodes <- unique(c(sub$col1, sub$col3))


nodes <- create_nodes(nodes=seq(uniquenodes), type="number", label=uniquenodes)

edges <- create_edges(from=match(sub$col1, uniquenodes),
  to=match(sub$col3, uniquenodes), rel="related")

g <- create_graph(nodes_df=nodes, edges_df=edges)