r - 如何结合使用 igraph 生成的图形和通过 cowplot::plot_grid 使用 ggplot2 制作的绘图
问题描述
最初,我无法完全绘制它,这意味着我找不到捕获绘图对象并将其提供给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)
解决方案
推荐阅读
- c++ - 将窗口大小从还原更改为最大化会导致某些内容出现在另一个内容之上
- unity3d - unity拖拽世界位置
- networking - 如何使用 mitmproxy 将 HTTPS 流量重定向到本地 HTTP 服务器?
- ruby-on-rails - 如何使用 rswag 在查询 api rspec 测试中传递字符串数组?
- css - CSS背景固定+封面拉伸图片(未正确覆盖)
- unity3d - unity粒子轮廓着色器
- flutter - 返回图表时出现索引错误
- python - 为什么我的 Django-Rest-Framework 发布方法没有字段?
- python - 在 Python 中的给定句子/字符串中排列哪个单词首先出现的列表
- facebook-graph-api - 无法通过 Facebook Graph API 向群组发帖