0

我有个问题。

如何使用 ggraph 复制网络图。plot(g) 来自 igraph 和 ggraph() 来自 ggraph 包。我得到了一个不同的数字。我正在使用权重,但是如何使用 ggraph 复制图表?

# library
library(igraph)
set.seed(1)
# create data:
links <- data.frame(
        source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"),
        target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I"),
        weight=(sample(1:20, 12, replace=T))

)
links$weight
nodes <- data.frame(
        name=LETTERS[1:10],
        carac=c( rep("young",3),rep("adult",2), rep("old",5))
)

# Turn it into igraph object
network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) 

# Make a palette of 3 colors
library(RColorBrewer)
coul  <- brewer.pal(3, "Set1") 

# Create a vector of color
my_color <- coul[as.numeric(as.factor(V(network)$carac))]

library(ggraph)
library(tidygraph)
#

g<-tbl_graph(nodes, links, directed = FALSE)
# igraph
plot(g)

#ggraph
g %>% ggraph() +
        geom_node_point() +
        geom_edge_link()+geom_node_point(aes(colour = carac))+ geom_node_text(aes(label = name), repel = TRUE) +
        theme_graph()


4

0 回答 0