在图形中使用简化时绘制图形的问题

Problem of plotting graph when I use simplify, in graph

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

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

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

情节 1:正确着色但有循环

图 2:使用 simplify

后着色错误

代码:

      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)

通常,在数据框中操作数据比在 igraph 属性中操作数据更容易。我建议在将其转换为图形之前准备好数据框中的所有内容。然后 simplify 将完成它应该做的工作,您可以根据需要绘制或分析图表。如果 remove.multipleTRUE,要保留 simplify 中的边缘属性,您需要定义 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()