Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ggraph/igraph中不考虑镶嵌面的节点_R_Ggplot2_Igraph_Ggraph - Fatal编程技术网

在ggraph/igraph中不考虑镶嵌面的节点

在ggraph/igraph中不考虑镶嵌面的节点,r,ggplot2,igraph,ggraph,R,Ggplot2,Igraph,Ggraph,我很难理解ggplot2和ggraph在刻面方面的区别。考虑以下数据: library(igraph) library(ggraph) library(ggplot2) df <- structure(list(x = c("H001", "H024", "H090", "H090", "H098", "H103", "H126", "H152", "H155", "H155", "B002", "B011", "B075",

我很难理解
ggplot2
ggraph
在刻面方面的区别。考虑以下数据:

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

也就是说,
facet\u var
的每个级别都有一些与之相关的点,并且只有这些点被绘制在相应的facet中。现在,如果我使用
ggraph
尝试类似的方法,我会遇到对我来说是奇怪的行为(尽管我显然承认这只是因为缺乏理解):


我认为这是故意的,你没有做错任何事

facet_edges
重复每个面板中的节点并绘制映射到变量的边

facet_节点
绘制映射到变量的节点,并绘制面板中存在终端节点的边

facet\u graph
——来自其文档——“处理所有这些问题,允许您定义行和列引用的数据类型,并基于每个面板中的节点过滤边(即使未绘制节点)。”


因此,
facet\u节点
facet\u图
的行为可能更像您预期的那样。

您需要将相关变量作为节点属性逐节点分配给facet。您的
facet\u var
变量是一个边缘属性;如果将其指定给每个二元体中的每个节点作为节点属性,则镶嵌面将起作用

facet\u图形
facets由两个变量(例如边属性和节点属性)构成。似乎
facet\u nodes
是您想要的,但是您需要向参数发送一个node属性,而不是edge属性

当前,
igraph
对象只有一个边属性:

> 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]

我应该提到,我确实尝试了
facet\u nodes()
并在combine\u vars(data,params$plot\u env,vars,drop=params$drop)中得到了这个错误
错误:在这种情况下,至少一个层必须包含用于facetting的所有变量
facet\u graph
可能更像您所期望的;编辑答案。使用您的示例数据为我工作。我使用了Github(1.0.0)中最新的R(3.4.2)、ggplot2(2.2.1)和GGRAPHE。同样,在问题中添加了会话信息。奇怪,太棒了。如果我这样做的话,我会发现设置
facet\u节点更加直观:
library(tidyverse)V(graph\u cors)$facet\u节点%gather(vars,values,-r,-facet\u var)%%>%distinct(values,.keep\u all=TRUE)%%>%pull(facet\u var)
。但这正是其他人也可以尝试这种方法的原因。如果您在这里绘制一些非IGRAPHE数据,您是否也可以使用
facet\u wrap
,只要facet是相同的名称?
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    
> 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]
> 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)