r - igraph graph.lattice 适用于所有组合(在 R 中)

标签 r igraph

问候:这个图似乎使用 R 的 igraph 包应该非常简单。我试图找出需要传递哪些参数来获取子模式和 super 模式之间的边。在此示例中,表示了 5 个项目的项目集。

item.lattice.plot

我确信这个问题可以通过 graph.lattice 解决函数位于 igraph但我还没有弄清楚要通过的论点。请注意,所提供的图表来自数据挖掘:概念和技术(Han、Kamber 和 Pei,2011 年)。

预先感谢您的任何指导。

最佳答案

我不认为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

关于r - igraph graph.lattice 适用于所有组合(在 R 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28774012/

相关文章:

r - geom_abline 多个斜率和截距

r - 多对多合并

python - 比较两个小有向图以及边和节点上的属性

r - 正确着色 R igraph 中的顶点

r - 如何迭代地定义映射参数到契约(Contract)顶点链?

r - R中条形图中的文本

r - 如何导入带有字符列的数据框(RPostgreSQL)?

r - 从包含它们的表中创建单个对象

r - 在保留节点名称/ID 的同时引入子图

r - 在 R 中读取数据集时出错