0

I have a question.

How can I replicate network graphs using ggraph. plot(g) is from igraph and ggraph()is from ggraph package. I got a different figure. I am using weight, but how can I replicate a graph using 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()


user224050
  • 317
  • 3
  • 10
  • 1
    What exactly are you trying to change? They have different ways of making plots—different default layouts, etc—so I'm not sure what you expect to be the same between the two – camille Feb 15 '20 at 20:00
  • I just want to replicate plot(g) in this example using ggraph. – user224050 Feb 15 '20 at 20:10
  • Is this correct? ``` graph <- as_tbl_graph(network) ggraph(graph, layout = 'fr', weights = weight) + geom_edge_link() + geom_node_point()+geom_node_point(aes(colour =carac ))+geom_node_text(aes(label = name), repel = TRUE) + theme_graph() ``` – user224050 Feb 15 '20 at 20:12
  • 1
    You can [edit] the post to include more code. It's unclear what you're asking though, because you seem to already have 2 plots but haven't said exactly what you want to change – camille Feb 15 '20 at 20:37

0 Answers0