briatte / ggnetwork

Geoms to plot networks with ggplot2
https://briatte.github.io/ggnetwork/
146 stars 28 forks source link

Edge selection #27

Closed SantiFilippo closed 5 years ago

SantiFilippo commented 6 years ago

Hi! First of all I want to thank you for the great job with this package. I am working with a weighted network of investments, and I was wondering whether a way exists to plot just part of the edges in the network, but keeping the nodes in the plot. (say, only those edges having weight greater than a minimum threshold). Is it possible to do it with ggnetwork? In case it is not, what package could fit for the purpose? thank you for any help

briatte commented 5 years ago

Hi @SantiFilippo

Many apologies for not answering your issue before.

As I'm sure you found out, yes, all network packages allow you to delete edges that you do not show in a graph. Below is an example with igraph (which can also be coded with the network package).

library(igraph)
library(ggnetwork)

n <- igraph::random.graph.game(10, 0.3)
E(n)$weight <- sample(c(1, 2), igraph::gsize(n), replace = TRUE)

# delete unwanted edges
n_for_plot <- delete_edges(n, which(E(n)$weight < 2))

# visualize plot-version of graph
ggplot(n_for_plot, aes(x, y, xend = xend, yend = yend)) +
  geom_edges() +
  geom_nodes() +
  theme_blank()