2

Greetings: This graph seems like something that should be pretty straightforward using the igraph package for R. I am trying to figure out which arguments I need to pass to obtain the edges between sub-patterns and super-patterns. In this example, a item set of 5 items is represented.

item.lattice.plot

I am sure this is solved with the graph.lattice function in igraph but I haven't figured out the arguments to pass. Please note that the graph presented comes from Data Mining: Concepts and Techniques (Han, Kamber, & Pei, 2011).

Thanks in advance for any guidance.

1 Answer 1

2

I don't think the graph.lattice is capable of creating this kind of graph. But you can define a new function:

graph.comb <- function(word) {
  # creates graph objects from character combination in word
  # example graph.comb("abc")

  do_layer <- function(words) {
    do.call( rbind, lapply(words, function(word){
      l_vec <- sapply(seq_len(nchar(word)), function(l) substring(word, l, l))
      w_comb <- apply(combn(l_vec, nchar(word)-1), 2, paste, collapse = "")
      w_df <- expand.grid(from = w_comb, to = word, stringsAsFactors = FALSE)
    }))
  }

  df_edges <- data.frame(from = word, to = NA, stringsAsFactors = FALSE)
  df2 <- df_edges
  while( min(nchar(df_edges$from)) > 0) {
    df2 <- do_layer(df2$from)
    df_edges <- rbind(df_edges, df2)
  }
  df_edges <- df_edges[complete.cases(df_edges), ]
  df_edges <- df_edges[!duplicated(df_edges), ]
  return(graph.data.frame(df_edges ))
}

Use it with any character string:

g1 <- graph.comb("abcd")
plot(g1, layout = layout.sugiyama(g1)$layout )

Result:

enter image description here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.