簡體   English   中英

使用R和ggplot2創建基於因子的樹狀圖

[英]creating a factor-based in dendrogram with R and ggplot2

這與其說是一種編碼,不如說是尋求幫助的一般方法;-)我准備了一張表格,其中包含有關生物的分類信息。 但是我想使用這些生物的“名稱”,因此沒有任何值或可以用來計算距離或聚類的任何值(這也是我所擁有的全部信息)。 我只想使用這些因素來創建一個顯示關系的圖。 我的數據如下所示:

    test2<-structure(list(genus = structure(c(4L, 2L, 7L, 8L, 6L, 1L, 3L, 
5L, 5L), .Label = c("Aminobacter", "Bradyrhizobium", "Hoeflea", 
"Hyphomonas", "Mesorhizobium", "Methylosinus", "Ochrobactrum", 
"uncultured"), class = "factor"), family = structure(c(4L, 1L, 
2L, 3L, 5L, 6L, 6L, 6L, 6L), .Label = c("Bradyrhizobiaceae", 
"Brucellaceae", "Hyphomicrobiaceae", "Hyphomonadaceae", "Methylocystaceae", 
"Phyllobacteriaceae"), class = "factor"), order = structure(c(1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Caulobacterales", 
"Rhizobiales"), class = "factor"), class = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alphaproteobacteria", class = "factor"), 
    phylum = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Proteobacteria", class = "factor")), .Names = c("genus", 
"family", "order", "class", "phylum"), class = "data.frame", row.names = c(NA, 
9L))

是否需要設置人工值來描述級別之間的距離?

這是嘗試使用data.tree庫

首先創建以下形式的字符串變量: Proteobacteria/Alphaproteobacteria/Caulobacterales/Hyphomonadaceae/Hyphomonas

library(data.tree)
test2$pathString <- with(test2, 
                               paste(phylum,
                                     class,
                                     order,
                                     family,
                                     genus, sep = "/"))

tree_test2 = as.Node(test2)
plot(tree_test2)

在此處輸入圖片說明

像這樣可以完成很多事情:

互動網絡:

library(networkD3)
test2_Network <- ToDataFrameNetwork(tree_test2, "name")
simpleNetwork(test2_Network)

在此處輸入圖片說明

或圖形樣式

library(igraph)
plot(as.igraph(tree_test2, directed = TRUE, direction = "climb"))

查看小插圖

在此處輸入圖片說明

使用ggplot2:

library(ggraph)
graph = as.igraph(tree_test2, directed = TRUE, direction = "climb")

ggraph(graph, layout = 'kk') + 
  geom_node_text(aes(label = name))+
  geom_edge_link(arrow = arrow(type = "closed", ends = "first",
                               length = unit(0.20, "inches"),
                               angle = 15)) +
  geom_node_point() +
  theme_graph()+
  coord_cartesian(xlim = c(-3,3), expand = TRUE)

在此處輸入圖片說明

也許:

ggraph(graph, layout = 'kk') + 
  geom_node_text(aes(label = name),   repel = T)+
  geom_edge_link(angle_calc = 'along',
                 end_cap = circle(3, 'mm'))+ 
  geom_node_point(size = 5) +
  theme_graph()+
  coord_cartesian(xlim = c(-3,3), expand = TRUE)

在此處輸入圖片說明

暫無
暫無

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

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