简体   繁体   English

如何将 Circlize 弦图另存为图形对象

[英]How to Save Circlize Chord Diagram as a graphic object

I created a chord diagram in R using the circlize package.我使用 circlize 包在 R 中创建了一个和弦图。 I noticed that this graphic is not being saved as a ggplot object.我注意到这个图形没有被保存为 ggplot 对象。 I am trying to make the graphic an object so I can export it to excel.我试图使图形成为一个对象,以便我可以将其导出到 excel。 Any suggestions?有什么建议?

chordDiagram(as.data.frame(ba_delivery_flow.sas7bdat), annotationTrack = "grid", preAllocateTracks =  1,
                        direction.type = "diffHeight")
circos.trackPlotRegion(track.index = 2, panel.fun = function(x, y) {
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
sector.name = get.cell.meta.data("sector.index")
circos.text(mean(xlim), ylim[1]+5, sector.name, facing = "clockwise", niceFacing = TRUE, adj = c(0, .5), cex = 1)
circos.axis(h = "top", labels.facing = "clockwise", labels.cex = 0.5, major.tick.percentage = 0.2, sector.index = sector.name, track.index = 2)

}, bg.border = NA)

R has two graphics systems, base and grid . R 有两个图形系统, basegrid ( grid is what ggplot2 and lattice are built on.) Your plot is built with base , and unfortunately base plots are not objects R, so you cannot assign them to a variable, and saving them is a little different. gridggplot2lattice的基础。)您的绘图是用base构建的,不幸的是, base绘图不是对象 R,因此您无法将它们分配给变量,并且保存它们有点不同。

To save the plot in a file, you open up a graphics device, like png , put your code to draw the plot, and then use dev.off() to close the device.要将绘图保存在文件中,您需要打开一个图形设备,例如png ,放置您的代码来绘制绘图,然后使用dev.off()关闭该设备。 Like this:像这样:

png("my_plot.png") 

## all of your plotting code

dev.off()

See ?png an argument list, and a few other devices too.请参阅?png参数列表,以及其他一些设备。


As an aside, you can save ggplots this way too.顺便说一句,您也可以ggplots这种方式保存ggplots If you have a plot object p , you can put print(p) in between the png() and the dev.off() .如果您有一个绘图对象p ,您可以将print(p)放在png()dev.off() This is essentially what ggsave does internally.这本质上是ggsave在内部所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM