Making Trees from Lists

I am working with the R programming language.

Suppose I have the following data frame that contains different food and drink choices:


data_frame = data.frame(food = c("pizza", "tacos", "nachos"),
 drinks = c("water", "coffee", "pop"))

I then made a "list" that contains every possible selection of these food and drink items:

lst1 <- lapply(data_frame, function(x) c("none", unlist(lapply(seq_len(length(x)), function(i) combn(x, i, FUN = paste, collapse = " & ")))))

My Question: I would like to take this above list and make a "tree" that shows every possible combination of food and drink choices that can be ordered together - this should look something like this:


Is it possible to do this in R using libraries such as "data.tree" and "ggplot2"?

I found this previous stackoverflow post (igraph graph.lattice for all combinations (in R) - Stack Overflow) - but I am not sure how to adapt the code provided there for my problem.

Can someone please show me how to do this?


I see a big difference between your verbal ask and your example chart. so there is too big a gap for me in my understanding of what you want to integrate those and imagine a full solution for you. I will reserve myself to simply providing a way to graph your chart example as that is a concrete goal that seems acheivable...
Here is an attempt to do that

svec <- c("white","blue","green","red","stripe")
pvec <- c("gray", "green", "black","blue")

(data_frame = expand_grid(shirt = svec,
                        pants = pvec) %>% mutate(result = paste(shirt,pants)))
myedges <- map(seq_len(nrow(data_frame)), ~ {row <- slice(data_frame,.x); c(row$shirt,row$result)}) %>% unlist()

(mygraph <- make_empty_graph() %>% add_vertices(nv = length(svec),name=svec ,color="red") %>%
    add_vertices(nv=nrow(data_frame),name=data_frame$result,color="green") %>%

E(mygraph)$label <- data_frame$pants
E(mygraph)$color <- "grey"

plot(mygraph, layout=-layout.reingold.tilford(mygraph) [, 2:1])

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.