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)
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)