briatte / ggnet

Network visualization with ggplot2
https://briatte.github.io/ggnet/
194 stars 33 forks source link

Is the ggnet2 function available for use ? #10

Closed ethen8181 closed 9 years ago

ethen8181 commented 9 years ago

Hi, sorry for bugging you with this minor problem. I couldn't use the ggnet2 function even after re-downloading the GGally package( ggnet works ). How can I fix this problem? Thanks beforehand!

briatte commented 9 years ago

Hi @ethen8181 -- you're slightly ahead of the tide: ggnet2 is still in the works, so it's not yet in the published version of GGally. I only just submitted it to the development branch :)

Based on the 99% coverage tests, I can confirm that the function works as expected, so you can use it immediately by sourcing the standalone function from this repository:

source("https://raw.githubusercontent.com/briatte/ggnet/master/R/ggnet2.R")

Or by installing both ggnet and ggnet2 as a package:

devtools::install_github("briatte/ggnet")
library(ggnet)

The vignette explains how every option works. There are some important differences with ggnet.

Please let me know if you find the code helpful. Thanks!

2015-10-22: edited to reflect packaged version.

ethen8181 commented 8 years ago

Hi briatte. Sorry fort the late response, it took me a while before I could finish the vignette. The plot that I was able to create by your function looks GREAT. One nitpick : Is it possible to make the edges shorter, so when creating directed edges the arrows won't overlay on top of the nodes?

bbc_network_plot

briatte commented 8 years ago

Hi @ethen8181,

Unfortunately, I have not yet found a convincing way to solve the arrow problem that you mention. It's mentioned in the known limitations at the end of the vignette, because the solution, if it exists, will be very hacky. Sorry! See below!

briatte commented 8 years ago

Hi @ethen8181,

I have added a new argument to both ggnet and ggnet2, called arrow.gap. It provides a fix for the edge arrows issue that we discussed earlier.

The fix is briefly described in the vignette: https://briatte.github.io/ggnet/#edge-arrows

Thanks to @heike, who provided the code for this fix.

ethen8181 commented 8 years ago

Hi, @briatte . Thanks for updating the the function!! I had a look and I wrote my thoughts in a pdf file in this link. Please let me know if you can't access to it ~

briatte commented 8 years ago

Hi @ethen8181

Thanks for the detailed review and solution.

Your code works fine once you take the theme part out of the plot (because it kills the arrows when you set line to element_blank()):

# edge lengths
x.length = with(edges, abs(X2 - X1))
y.length = with(edges, abs(Y2 - Y1))

k = 10
x.length = cut_interval(x.length, k, labels = 1:k)
y.length = cut_interval(y.length, k, labels = 1:k)

# generate a arrow gap ratio with the same length as the
# number_of_splits for mapping the appropriate ratio later
# the longer the distance, the smaller ratio it gets
arrow.gap = rev(seq(.9, .95, length.out = k))

# obtain the actual gap ratio for each edge
x.length = arrow.gap[ x.length ]
y.length = arrow.gap[ y.length ]

# adjust both the head and tail of the edges
segment2 = transform(edges,
                     X2 = X1 + x.length * (X2 - X1),
                     Y2 = Y1 + y.length * (Y2 - Y1),
                     X1 = X2 + x.length * (X1 - X2),
                     Y1 = Y2 + y.length * (Y1 - Y2))

ggplot(segment2) +
  geom_curve(aes(x = X1, xend = X2, y = Y1, yend = Y2),
             arrow = arrow(length = unit(0.3, "cm")),
             size = 0.5, curvature = 0.1) +
  geom_point(data = data.frame(coordinates), aes(x, y), size = 4,
             color = "cyan4", alpha = .4) +
  theme_bw()

Here's the result with the development version of ggplot2, using the new curve geom to illustrate another idea (using curved edges to avoid edge overplotting):

rplot

Questions:

  1. Changing the number of splits does not seem to have much effect, at least not in the Coleman example. I tried setting k to 1, 10 or 100 and got nearly identical results.
  2. I've updated the code of ggnet2 to use your solution:
    • Are you satisfied with that implementation?
    • Can I ask for your full name in order to include you among the co-authors?
ethen8181 commented 8 years ago

Hi @briatte . The geom_curve is really neat!~ Hope it gets deployed on CRAN soon.

Q2.1. I tried the ggnet2 function on another network, it seems to being working, I'll let you know if it doesn't work for other networks or if there's a better way to do this! Q2.2. The full name is Ming-Yu Liu. Glad it worked out, though I really didn't contributed too much ... Many thanks for creating the function ~ network_plot

briatte commented 8 years ago

I like geom_curve very much too, and will probably add it as an option in ggnet and ggnet2 when it gets published.

You and Heike are now acknowledged as contributors in the documentation.

Your network plot looks cool!

briatte commented 8 years ago

P.S. You might find this repo interesting:

https://github.com/briatte/ggnetwork

It's the same idea as ggnet, but using only "pure" ggplot2 geoms.

heike commented 8 years ago

François,

I've seen it, and I like the solution - what are your plans with it?

Heike

On Mon, Oct 12, 2015 at 12:14 AM, François Briatte <notifications@github.com

wrote:

P.S. You might find this repo interesting:

https://github.com/briatte/ggnetwork

It's the same idea as ggnet, but using only "pure" ggplot2 geoms.

— Reply to this email directly or view it on GitHub https://github.com/briatte/ggnet/issues/10#issuecomment-147292616.

briatte commented 8 years ago

Hi @heike,

I don't really have a plan for ggnetwork. It passes CRAN checks, but it's more an experiment in aliasing a few geoms than a real package. Could it find its place in the paper?

briatte commented 8 years ago

Dear @ethen8181 and @heike

I have just spent one hour looking at the results of your respective methods to solve the "edge arrows" issue, and it looks like the solution by @heike is more resilient to different network sizes and layouts.

Here are some tests based on a random graph and on an example suggested by Heike. Sorry @ethen8181, vector algebra wins :)

circle_e circle_h fr_e fr_h rand_e rand_f

rnd_fr_e rnd_fr_h rnd_kama_e rnd_kama_f rnd_rand_e rnd_rand_f

ethen8181 commented 8 years ago

@briatte Noted. My solution of directly setting the arrow.gap to be numbers between .9, .95 is probably a bad idea. I'll come back to it if I come across a better solution. Thanks for the update ~