Reputation: 5907
I am interested in making a picture like this:
Basically, I would like to show all the combinations possible. I tried to do this with ggplot2, but I can't find an exact function that makes this tree.
Is there a straightforward way to do this?
I created some data like this:
var1 <- sample( LETTERS[1:5], 100, replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2) )
var2 <- sample( LETTERS[6:7], 100, replace=TRUE, prob=c(0.5, 0.5) )
var3<- sample( LETTERS[8:11], 100, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) )
f <- data.frame(var1, var2, var3)
f$var1 = as.factor(f$var1)
f$var2 = as.factor(f$var2)
f$var3 = as.factor(f$var3)
I tried:
plot(f)
But this didn't work. Thanks!
Upvotes: 2
Views: 227
Reputation: 7597
The R-graph gallery gives us a really good starting point.
library(ggraph)
library(igraph)
library(tidyverse)
d1 <- data.frame(from="origin", to=LETTERS[1:5])
d2 <- data.frame(from=rep(d1$to, each=2), to=paste(LETTERS[6:7], 1:10, sep="_"))
d3 <- data.frame(from=rep(d2$to, each=3), to=paste(LETTERS[8:10], 1:30, sep="_"))
edges <- do.call(rbind, list(d1, d2, d3))
mygraph <- graph_from_data_frame(edges)
# Basic tree
ggraph(mygraph, layout = 'dendrogram', circular = FALSE) +
geom_edge_diagonal() +
geom_node_point() +
geom_node_text(aes(label = c("origin", gsub("[0-9]|_","",edges$to))), repel = TRUE, colour = 'blue') +
theme_void()
Upvotes: 2