1

我想绘制我的图表。我提供了一个样本数据集。我用simplemented来去除循环,但它完全影响了数据的结构。当我在不使用简化的情况下绘制数据时,我有正确的顶点和边着色,但有循环。

我使用简化来删除循环和它之后的颜色,这是错误的,因为它的每个节点和边缘都应该具有与我在代码中定义的颜色相同的颜色。

有谁知道如何删除情节中的循环而不影响数据的结构?

图 1:正确着色但有循环

在此处输入图像描述

图 2:使用简化后的错误着色

在此处输入图像描述

编码:

      X user.screen_name           child       parent in_reply_to_screen_name vaccine_type label
1     0   TweeetLorraine 1.392218e+18 1.392218e+18                       1  AstraZeneca     0
2     1       phldenault 1.393259e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2
41   40  ElizabethDuncan 1.392297e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2
42   41          7Rose75 1.392294e+18 1.392218e+18          TweeetLorraine  AstraZeneca     1
43   42      wh0careswh0 1.392336e+18 1.392294e+18                 7Rose75  AstraZeneca     0
44   43   T_ProudVeteran 1.392330e+18 1.392294e+18                 7Rose75  AstraZeneca     2
45   44   TweeetLorraine 1.392294e+18 1.392294e+18                 7Rose75  AstraZeneca     2
46   45         Norlaine 1.392288e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2
47   46    elham95264575 1.393212e+18 1.392288e+18                Norlaine  AstraZeneca     1
48   47      soyfreemike 1.392387e+18 1.392288e+18                Norlaine  AstraZeneca     0
49   48          KMTCarr 1.392288e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2
50   49     angela_petta 1.392283e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2
51   50     lhoneyimhome 1.392272e+18 1.392218e+18          TweeetLorraine  AstraZeneca     2



net1 <- graph_from_data_frame(df %>% select("child","parent")) 
rel = get.adjacency(graph, sparse = FALSE) 

graph = simplify(net1, remove.loops=TRUE)
graph
summary(graph)


vertex_attr(graph, "label") <- df$label
#Set edge attribute:
edge_attr(graph, "label") <- df$label




E(graph)$color[E(graph)$label == 2] <- '#B3DE69' #green
E(graph)$color[E(graph)$label == 1] <- '#80B1D3' #yellow
E(graph)$color[E(graph)$label == 0] <- '#FB8072'#purple

V(graph)$color[V(graph)$label == 2] <- '#B3DE69'
V(graph)$color[V(graph)$label == 1] <- '#80B1D3'
V(graph)$color[V(graph)$label == 0] <- '#FB8072'
g<-c('#B3DE69','#80B1D3','#FB8072')
plot(graph,layout=layout.fruchterman.reingold,
     vertex.frame.color=NA,vertex.label.color="black",
     edge.label = NA,
     vertex.size=3, usecurve=TRUE,
     edge.lwd=0.02,
     vertex.dist=10,vertex.label.dist=2,vertex.label.cex=0.9,
     pad=0.9,alpha=80,
     edge.arrow.size=.1)


legend("bottomleft",legend= c("Positive","Neutral","Negative"),
       col=g,pch=19,pt.cex=1.5,bty="n",
       title="Label category")

title(main="Visualization ", cex.main=1)

4

1 回答 1

1

通常,在数据框中操作数据比在 igraph 属性中更容易。我建议在将其转换为图表之前准备好数据框中的所有内容。然后simplify将按应有的方式完成工作,您可以根据需要绘制或分析图表。simplify要保留if remove.multipleis中的边缘属性TRUE,您需要定义edge.attr.comb参数。下面我使用dplyr::first了 ,这意味着我们在组合多个边时选择第一个值。

编辑:使用 OP 数据并保留边缘属性simplify

library(igraph)
library(dplyr)
library(magrittr)
library(tibble)
library(rlang)
library(readr)

df <- read_tsv('so_user142_data.tsv', col_types = cols())

color_map <- c(
    '0' = '#B3DE69', # green
    '1' = '#80B1D3', # blue
    '2' = '#FB8072'  # salmon
)

df %<>%
    mutate(label = recode(label, !!!color_map)) %>%
    rename(color = label) %>%
    select(
        child = user.screen_name,
        parent = in_reply_to_screen_name,
        color
    )


vertex_colors <-
    bind_rows(
        df %>% select(name = child, color),
        df %>% select(name = parent, color)
    ) %>%
    group_by(name) %>%
    summarize_all(first) %>%
    ungroup

g <-
    df %>%
    graph_from_data_frame(vertices = vertex_colors) %>%
    simplify(edge.attr.comb = first)

png('so_user142_graph.png', 800, 800)

    plot(
        g,
        layout = layout.fruchterman.reingold,
        vertex.frame.color = NA,
        vertex.label.color = 'black',
        vertex.size = 7,
        edge.curved = TRUE,
        edge.lwd = 0.4,
        vertex.dist = 10,
        vertex.label.dist = 1.2,
        vertex.label.cex = 1.2,
        pad = 0.9,
        alpha = 80,
        edge.arrow.size = 1.
    )

dev.off()

如上图所示

于 2021-05-20T18:12:51.783 回答