所有组合的igraph graph.lattice(在R中)

时间:2015-02-27 20:48:56

标签: r igraph

问候:这个图看起来像使用igraph包的R应该非常简单。我试图弄清楚我需要传递哪些参数来获得子模式和超模式之间的边缘。在该示例中,表示了5个项目的项目集。

item.lattice.plot

我确信这是通过graph.lattice中的igraph函数解决的,但我还没有找到要通过的参数。请注意,所提供的图表来自 Data Mining:Concepts and Techniques (Han,Kamber,& Pei,2011)。

提前感谢任何指导。

1 个答案:

答案 0 :(得分:1)

我认为graph.lattice无法创建此类图表。但是你可以定义一个新函数:

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 ))
}

将它与任何字符串一起使用:

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

结果:

enter image description here