簡體   English   中英

節點不尊重ggraph / igraph中的刻面

[英]Nodes not respecting facetting in ggraph/igraph

我在理解ggplot2ggraphggplot2方面的區別時遇到了一些麻煩。 請考慮以下數據:

library(igraph)
library(ggraph)
library(ggplot2)

df <- structure(list(x = c("H001", "H024", "H090", "H090", "H098", 
                     "H103", "H126", "H152", "H155", "H155", "B002", "B011", "B075", 
                     "B092", "M002", "M002", "M002", "M050", "M050", "M085", "M085", 
                     "M247", "M247", "M247"), 
               y = c("H103", "H126", "H152", "H155","H155", "H001", "H024", "H090", "H090", "H098", "B092", "B075", 
                      "B011", "B002", "M050", "M085", "M247", "M002", "M247", "M002", "M247", "M002", "M050", "M085"), 
               r = c(0.963248925980302, 0.991452643542894, 0.965947578382865, 
                     0.963234153063794, 0.962277411462605, 0.963248925980302, 
                     0.991452643542894, 0.965947578382865, 0.963234153063794, 
                     0.962277411462605, 0.960948147492217, 0.957371360458182, 
                     0.957371360458182, 0.960948147492217, 0.96976135236222, 
                     0.977435712803837, 0.997037031981303, 0.96976135236222, 
                     0.978553503235858, 0.977435712803837, 0.992741796542001, 
                     0.997037031981303, 0.978553503235858, 0.992741796542001), 
               facet_var = c("08MH", "08MH", "08MH", "08MH", "08MH", "08MH", "08MH", "08MH", "08MH", 
                             "08MH", "08HB", "08HB", "08HB", "08HB", "08NM", "08NM", "08NM", "08NM", 
                             "08NM", "08NM", "08NM", "08NM", "08NM", "08NM")), 
          class = "data.frame", row.names = c(NA, -24L), .Names = c("x", "y", "r", "facet_var")
)

如果我使用一個簡單的ggplot繪制數據,我可以采用一種我認為“正常”的方式:

ggplot(df, aes(x = x, y = y)) +
         geom_point() +
         facet_wrap(~facet_var)

在此輸入圖像描述

也就是說, facet_var每個級別都有一些與之關聯的點,並且只有這些點被繪制在相應的facet中。 現在,如果我嘗試使用ggraph的類似方法,我會遇到奇怪的行為(盡管我明確承認它只是反映了缺乏理解):

graph_cors <- graph_from_data_frame(df, directed = FALSE)

ggraph(graph_cors) +
  geom_edge_link(aes(edge_alpha = abs(r), color = r), edge_width = 2) +
  guides(edge_alpha = "none") +
  scale_edge_colour_gradientn(limits = c(-1, 1), colors = topo.colors(5)) +
  geom_node_point(color = "black", size = 5) +
  geom_node_text(aes(label = name), repel = TRUE) +
  facet_edges(~facet_var)

在此輸入圖像描述

因此,盡管邊緣相對於刻面,但是在每個刻面中繪制所有節點。 誰能勾勒出我在這里做錯了什么?

會話信息

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggraph_1.0.0    ggplot2_2.2.1   igraph_1.1.2    testthat_1.0.2  devtools_1.13.3

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13          compiler_3.4.2        plyr_1.8.4            bindr_0.1             viridis_0.4.0        
 [6] tools_3.4.2           digest_0.6.12         viridisLite_0.2.0     memoise_1.1.0         tibble_1.3.4         
[11] gtable_0.2.0          pkgconfig_2.0.1       rlang_0.1.2           rstudioapi_0.7.0-9000 ggrepel_0.7.0        
[16] yaml_2.1.14           bindrcpp_0.2          gridExtra_2.3         withr_2.0.0           dplyr_0.7.4          
[21] grid_3.4.2            glue_1.1.1            R6_2.2.2              tweenr_0.1.5          udunits2_0.13        
[26] magrittr_1.5          scales_0.5.0          fortunes_1.5-4        MASS_7.3-47           units_0.4-6          
[31] assertthat_0.2.0      swtext_0.0.1          ggforce_0.1.1         colorspace_1.3-2      lazyeval_0.2.0       
[36] munsell_0.4.3         crayon_1.3.4    

您需要將相關變量作為節點屬性分配給節點的構面。 你的facet_var變量是一個edge屬性; 如果將其作為節點屬性分配給每個dyad中的每個節點,則可以使用分面。

facet_graph兩個變量構成(例如,邊緣屬性和節點屬性)。 看起來像facet_nodes是你想要的,但是你需要向參數發送一個node屬性,而不是一個edge屬性。

目前,您的igraph對象只有一個edge屬性:

> graph_cors
IGRAPH UN-- 16 24 -- 
+ attr: name (v/c), r (e/n), facet_var (e/c)
+ edges (vertex names):
 [1] H001--H103 H024--H126 H090--H152 H090--H155 H098--H155 H001--H103 H024--H126 H090--H152
 [9] H090--H155 H098--H155 B002--B092 B011--B075 B011--B075 B002--B092 M002--M050 M002--M085
[17] M002--M247 M002--M050 M050--M247 M002--M085 M085--M247 M002--M247 M050--M247 M085--M247

graph_cors <- graph_from_data_frame(df, directed = FALSE)

facet_nodes <- cbind(c(df[,1], df[,2]), 
                     df[match(c(df[,1], df[,2]), df[,1]),4])

# Assign the facet_var variable to corresponding dyad pairs
V(graph_cors)$facet_node <- facet_nodes[match(V(graph_cors)$name, facet_nodes[,1]),2]

現在, igraph對象有一個節點屬性(“facet_node”):

> graph_cors
IGRAPH UN-- 16 24 -- 
+ attr: name (v/c), facet_node (v/c), r (e/n), facet_var (e/c)
+ edges (vertex names):
 [1] H001--H103 H024--H126 H090--H152 H090--H155 H098--H155 H001--H103 H024--H126 H090--H152
 [9] H090--H155 H098--H155 B002--B092 B011--B075 B011--B075 B002--B092 M002--M050 M002--M085
[17] M002--M247 M002--M050 M050--M247 M002--M085 M085--M247 M002--M247 M050--M247 M085--M247

# Use the facet_nodes argument with the nodal attribute
ggraph(graph_cors) +
  geom_edge_link(aes(edge_alpha = abs(r), color = r), edge_width = 2) +
  guides(edge_alpha = "none") +
  scale_edge_colour_gradientn(limits = c(-1, 1), colors = topo.colors(5)) +
  geom_node_point(color = "black", size = 5) +
  geom_node_text(aes(label = name), repel = TRUE) +
  facet_nodes(~facet_node)

在此輸入圖像描述

我認為這是設計上的,你沒有做任何“錯誤”。

facet_edges在每個面板中重復節點,並繪制映射到變量的邊。

facet_nodes繪制映射到變量的節點,並繪制面板中存在的終端節點的邊。

facet_graph - 來自其文檔 - “處理所有這些問題,允許您定義行和列引用的數據類型,以及根據每個面板中的節點過濾邊緣(即使未繪制節點)。 “

因此, facet_nodesfacet_graph的行為可能更像您的預期。

暫無
暫無

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

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