簡體   English   中英

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

[英]igraph graph.lattice for all combinations (in R)

問候:使用R的igraph程序包,該圖看起來應該很簡單。我試圖弄清楚我需要傳遞哪些參數以獲得子模式和超級模式之間的邊緣。 在此示例中,代表了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 )

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM