Why you want to turn igraph network polot to ggplot?
Flexibility: ggplot is a very flexible and customizable plotting package that allows you to create high-quality, publication-ready plots with a high degree of control over the visual aesthetics of your plots. You can easily modify various aspects of the plot, such as the color, shape, and size of the nodes and edges, and the placement of the labels.
Integration with igraph: ggplot works seamlessly with igraph, making it easy to create complex and informative visualizations of your network data. You can use ggplot to visualize network data in a variety of ways, including heatmaps, scatterplots, and bar charts.
Consistency: ggplot provides a consistent grammar for building plots, which makes it easy to create plots with a consistent style and look across different datasets. This can be particularly useful if you are working with multiple datasets and want to create a consistent visual language for your plots.
Reproducibility: ggplot produces code that can be easily reproduced, making it easier to share and collaborate on your work. You can also easily modify and update your plots as your data or analysis changes.
Overall, using ggplot to plot igraph results can help you create informative, visually appealing, and reproducible visualizations of your network data.
# data clean dataUU <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyUndirectedUnweighted.csv", header=TRUE) TB <- na.omit(melt(dataUU)) TB$from <- str_replace_all(TB$from, ' ', '.') # width of the edges set.seed(3) TB$value = runif(nrow(TB), min=0, max=10) # size of pointsd TB_size <- as.data.frame(table(c(as.matrix(TB[1:2]))))
network=graph_from_data_frame(TB[1:2] )
set.seed(1) e <- get.edgelist(network,names=FALSE) l <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(network), area=30*(vcount(network)^2), repulse.rad=(vcount(network)^4)) plot(network, layout=l, vertex.size=4, vertex.label=NA, edge.arrow.size= 0, )
## convert the layout to a data.frame fr.all.df <- as.data.frame(l) ## add in the species codes fr.all.df$species <- V(network)$name ## add size for each nodes fr.all.df$size <- TB_size$Freq[match(fr.all.df$species, TB_size$Var1)]
g <- TB[1:2] colnames(g) <-c('from', 'to') g$weight = TB[[3]] g$from.x <- fr.all.df$V1[match(g$from, fr.all.df$species)] # match the from locations from the node data.frame we previously connected g$from.y <- fr.all.df$V2[match(g$from, fr.all.df$species)] g$to.x <- fr.all.df$V1[match(g$to, fr.all.df$species)] # match the to locations from the node data.frame we previously connected g$to.y <- fr.all.df$V2[match(g$to, fr.all.df$species)]