【发布时间】:2019-11-25 05:24:08
【问题描述】:
最初,我无法完全绘制它,这意味着我找不到捕获绘图对象并将其提供给cowplot::plot_grid 的方法。现在,我找到了一种解决方法,可以将图形图的图像保存为 png 并使用cowplot::draw_image 读取它。有没有更简单的方法来做到这一点?此外,很难设置 png 的参数以具有良好的分辨率和大小并避免边缘修剪。我还需要对图进行一些调整,例如,应该可以使用具有精确连接权重值的自环和定向箭头。
以下是我得到的两个选项及其各自的结果。
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()
选项 1 - 直接
# option 1 - empty graph
cowplot::plot_grid(plot(graph_1), gg_test)
选项 2 - 归档
# 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)
【问题讨论】:
-
这不是很有效,但也许你可以找到一种方法让它变得更好:在你制作图表之后用户
x <- recordPlot(),然后plot_grid(x, gg_test)。但是,恐怕结果并不令人印象深刻。你试过ggnet2吗? -
recordPlot()有效,但它需要gridGraphics包,并且(在我的情况下)在边缘添加NA标签。质量方面还是不错的!我不知道ggnet2我害怕。我会检查一下;) -
是的,NA 加上混乱的节点和箭头样式,不确定您是否可以轻松处理。
-
如果 gridGraphics 弄乱了你的绘图,请在 github 上提出问题。目标是忠实地捕捉所有基本图形,但仍有一些极端情况需要修复。
-
NAs 的问题最近在 gridGraphics 中得到修复:github.com/pmur002/gridgraphics/issues/14