【问题标题】:revealing clusters of interaction in igraph揭示 igraph 中的交互集群
【发布时间】:2016-11-04 21:08:24
【问题描述】:

我有一个交互网络,我使用以下代码制作邻接矩阵,然后计算网络节点之间的相异度,然后将它们聚类形成模块:

ADJ1=abs(adjacent-mat)^6
dissADJ1<-1-ADJ1
hierADJ<-hclust(as.dist(dissADJ1), method = "average")

现在我希望在绘制 igraph 时出现这些模块。

g<-simplify(graph_from_adjacency_matrix(adjacent-mat, weighted=T))
plot.igraph(g)

但是,到目前为止,我发现将 hclust 输出转换为图形的唯一方法是按照以下教程:http://gastonsanchez.com/resources/2014/07/05/Pretty-tree-graph/

phylo_tree = as.phylo(hierADJ)
graph_edges = phylo_tree$edge
graph_net = graph.edgelist(graph_edges)
plot(graph_net)

这对分层沿袭很有用,但我只希望与集群密切交互的节点如下:

谁能推荐如何使用命令(例如 igraph 中的组件)来显示这些集群?

【问题讨论】:

    标签: r cluster-analysis igraph hierarchical-clustering


    【解决方案1】:

    igraph 提供了一堆不同的layout algorithms,用于在图中放置节点。

    force-directed layout(由 igraph 中的layout.fruchterman.reingold 实现)是一个很好的起点,可以像这样的加权网络。

    以下是使用一些简单的模拟数据使用力导向布局的示例。

    首先,我们创建一些模拟数据和集群,以及一些“噪音”以使其更真实:

    library('dplyr')
    library('igraph')
    library('RColorBrewer')
    
    set.seed(1)
    
    # generate a couple clusters
    nodes_per_cluster <- 30
    n <- 10
    
    nvals <- nodes_per_cluster * n
    
    # cluster 1 (increasing) 
    cluster1 <- matrix(rep((1:n)/4, nodes_per_cluster) + 
                       rnorm(nvals, sd=1),
                       nrow=nodes_per_cluster, byrow=TRUE)
    
    # cluster 2 (decreasing)
    cluster2 <- matrix(rep((n:1)/4, nodes_per_cluster) + 
                       rnorm(nvals, sd=1),
                       nrow=nodes_per_cluster, byrow=TRUE)
    
    # noise cluster
    noise <- matrix(sample(1:2, nvals, replace=TRUE) +
                    rnorm(nvals, sd=1.5),
                    nrow=nodes_per_cluster, byrow=TRUE)
    
    dat <- rbind(cluster1, cluster2, noise)
    colnames(dat) <- paste0('n', 1:n)
    rownames(dat) <- c(paste0('cluster1_', 1:nodes_per_cluster), 
                       paste0('cluster2_', 1:nodes_per_cluster),
                       paste0('noise_',    1:nodes_per_cluster))
    

    接下来,我们可以使用Pearson correlation来构造我们的邻接矩阵:

    # create correlation matrix
    cor_mat <- cor(t(dat))
    
    # shift to [0,1] to separate positive and negative correlations
    adj_mat <- (cor_mat + 1) / 2
    
    # get rid of low correlations and self-loops
    adj_mat <- adj_mat^3
    adj_mat[adj_mat < 0.5] <- 0
    diag(adj_mat) <- 0
    

    使用hclustcutree 对数据进行聚类:

    # convert to dissimilarity matrix and cluster using hclust
    dissim_mat <- 1 - adj_mat
    
    dend <- dissim_mat %>% 
        as.dist %>% 
        hclust
    
    clusters = cutree(dend, h=0.65)
    
    # color the nodes
    pal = colorRampPalette(brewer.pal(11,"Spectral"))(length(unique(clusters)))
    node_colors <- pal[clusters]
    

    最后,从邻接矩阵创建一个 igraph 图并使用fruchterman.reingold 布局进行绘制:

    # create graph
    g <- graph.adjacency(adj_mat, mode='undirected', weighted=TRUE)
    
    # set node color and plot using a force-directed layout (fruchterman-reingold)
    V(g)$color <- node_colors
    coords_fr = layout.fruchterman.reingold(g, weights=E(g)$weight)
    
    # igraph plot options
    igraph.options(vertex.size=8, edge.width=0.75) 
    
    # plot network
    plot(g, layout=coords_fr, vertex.color=V(g)$color)
    

    在上面的代码中,我生成了两个相关行的“簇”和第三组“噪声”。

    层次聚类(hclust + cuttree)用于将数据点分配给聚类,并根据聚类成员进行着色。

    结果如下:

    有关使用 igraph 进行聚类和绘制图形的更多示例,请查看:http://michael.hahsler.net/SMU/LearnROnYourOwn/code/igraph.html

    【讨论】:

    • 非常感谢,这些图表看起来非常有用,我的经验还不够高,无法在截止日期前制作出类似的东西,但我可能会尝试与您联系以发送一些示例数据
    【解决方案2】:

    您没有共享一些玩具数据供我们使用并建议改进代码,但您的问题表明您只对清晰地绘制集群感兴趣 - 即图形表示。

    虽然igraph 带有一些不错的force directed layout algorithms,例如layout.fruchterman.reingoldlayout_with_kk 等,但是在存在大量节点的情况下,它们很快就会变得难以解释和理解全部。

    像这样:

    通过这些传统的网络可视化方法,

    • 布局算法而非数据决定了可视化效果
    • 相似的网络最终可能以非常不同的方式呈现
    • 大量节点会使可视化难以解释

    相反,我发现Hive Plots 更擅长显示重要的网络属性,在您的实例中,这些属性是集群和边缘。

    在你的情况下,你可以:

    • 在不同的直线上绘制每个聚类
    • 智能地对节点的放置进行排序,以便将具有某些属性的节点放置在每条直线的最末端或起点
    • 为边缘着色以识别边缘方向

    要实现这一点,您需要:

    • 使用 ggnetwork 包将您的 igraph 对象转换为数据框
    • 将您的集群映射到此数据帧中存在的节点
    • 为直线生成坐标并将其映射到每个集群
    • 使用ggplot 进行可视化

    R 中还有一个hiveR 包,如果您希望使用打包的解决方案。您可能还会发现另一种非常有用的图表可视化技术:BioFabric

    【讨论】:

    • 非常感谢,这些图表看起来非常有用,我的经验还不够高,无法在截止日期前制作出类似的东西,但我可能会尝试与您联系以发送一些示例数据
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2018-06-02
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多