A S
A S

Reputation: 1235

Plotting a tree in R given existing values

Let's say I have an existing numeric vector num <- c(1, 2, 3, 4, 5, 6, 7) and I need to visualize it in the simplest way possible (every node have two children) -- like this (sorry, I'm definitely not an artist):

sample tree

Is it possible with R?

Upvotes: 0

Views: 102

Answers (1)

lukeA
lukeA

Reputation: 54237

Try this:

num <- c(1, 2, 3, 4, 5, 6, 7)
library(igraph)
library(psych)
g <- graph.edgelist(matrix(c(rep(seq_len((length(num)-1) / 2), each = 2), num[-1]), ncol = 2))
plot(g, layout = factor.rotate(layout.reingold.tilford(g), angle = -90), ylim = c(1, -1))

enter image description here

Upvotes: 2

Related Questions