4

Initially, I could not plot it completely, meaning that I could not find a way to capture the plot object and feed it to cowplot::plot_grid. Now, I found a workaround which saves to file the image of the graph plot as png and read it with cowplot::draw_image. Is there a simpler way to do it? Also, it is difficult to set the parameters of the png in order to have good resolution and size and avoid the margin trimming. I also need some tweaking on the plot, e.g., self-loop and directed arrows with precise values of connection weights should be possible.

In the following, there are two options and their respective results I obtain.

library(ggplot2); library(cowplot); library(igraph)
graph_1 <- sample_gnm(10, 25, directed = T, loops = T)
gg_test <- ggplot(data.frame("a" = seq(1, 5, length.out = 10), "b" = runif(10)), aes(x=a, y=b)) + geom_point() + theme_classic()

Option 1 - direct

# option 1 - empty graph
cowplot::plot_grid(plot(graph_1), gg_test)

opt1

Option 2 - to file

# option 2 - working but horrible code and difficult setting of the resolution/size (r-base not the best)
png("to_delete_for_import.png", res = 150, height = 800, width = 1100)
plot(graph_1, edge.label = LETTERS[1:10], vertex.color = RColorBrewer::brewer.pal(10, "Spectral"))
dev.off()
graph_1_cwpl <- ggdraw() + draw_image("to_delete_for_import.png")
file.remove("to_delete_for_import.png")
cowplot::plot_grid(graph_1_cwpl, gg_test)

opt2

Garini
  • 1,088
  • 16
  • 29
  • 2
    This does not really work well, but maybe you can find a way of making it better: user `x <- recordPlot()` after you make the graph and then `plot_grid(x, gg_test)`. However, the result is not impressive, I'm afraid. Did you try `ggnet2`? – January Jul 16 '19 at 13:04
  • `recordPlot()` works but it needs `gridGraphics` package and it is (in my case) adding `NA` labels on the edges. Quality-wise it is fine though! I do not know `ggnet2` I am afraid. I will check it out ;) – Garini Jul 16 '19 at 13:53
  • Yeah, the NAs plus messed up node and arrow styles, not sure you can easily deal with that. – January Jul 16 '19 at 13:59
  • If gridGraphics messes up your plot, please file an issue on github. The goal is to faithfully capture all base graphics, but there are some corner cases that still need fixing. – Claus Wilke Jul 16 '19 at 15:20
  • I will open the issue. It seems that `ggnet2` (which is quite good) can not handle self-loops that are heavily present in my real application. On the merit, I opened an issue on the GitHub page. – Garini Jul 16 '19 at 16:15
  • 1
    The issue with `NA`s was recently fixed in gridGraphics: https://github.com/pmur002/gridgraphics/issues/14 – Claus Wilke Aug 17 '19 at 18:00

1 Answers1

2

I recently had the same issue and found the following solutions to be helpful. The first approach is similar to what user @January already commented:

library(ggplotify) 
E(graph_1)$label <- ""
plot_grid(base2grob(~plot(graph_1)),
          gg_test)

enter image description here

And here is the second approach using ggraph:

library(ggraph)
ggtest2 <- ggraph(graph_1) +
          geom_node_point() +
          geom_edge_link() +
          theme_classic()

plot_grid(ggtest2, gg_test)

enter image description here

Community
  • 1
  • 1
Ben Nutzer
  • 1,082
  • 7
  • 15
  • It is not what I am looking for because it still needs to manually silence the labels and I hope there is another way, also considering the number of tools there are for network drawing. Moreover, I do not know how the grob would behave in the case of more complex graph. The same for the second one, but I will take a look if I can improve its aesthetic to the point I need it. – Garini Jul 16 '19 at 16:49