How do I export a KaryoploteR plot or insert it into a grid/lattice? Can I assign a KaryoploteR plot to a variable?
2
2
Entering edit mode
3.3 years ago
Andrew ▴ 20

I've generated some KaryoploteR plots I want to use, but I have a lot of them to handle. I'd like to be able to organize them in a grid or at least export them in code. But if I assign the output to a variable, it seems like it just assigns parameter data and doesn't assign a graphical object to the parameter.

I looked through the code, and it appears to call graphics::plot() to generate the graphics. I'm not super familiar with the underlying graphics handling in R. Is there a way to assign KaryoploteR plots to figures so I can plot them within a grid or export them using the png or tiff saving functions?

karyoploteR grid ideograms • 2.2k views
ADD COMMENT
0
Entering edit mode

Thank you for explaining the issue in detail. It would help if you could post some example code where help is needed.

ADD REPLY
1
Entering edit mode
3.3 years ago
Lisa Ha ▴ 120

I had the same question recently and just found this blog post which looks very promising: Save base graphics as pseudo objects in R. It describes two approaches for base graphics, although one is based on a now obsolete package (pryr). The current implementation seems to be tidyeval. I haven't tried it yet, but it looks like what you need.

ADD COMMENT
1
Entering edit mode

I just found this on the karyoplotr github page: ggplotify or grid to use karyoplotr images in multipanel layouts

ADD REPLY
0
Entering edit mode
3.3 years ago
Andrew ▴ 20

Code:

kp <- plotKaryotype(plot.type = 1, chromosomes = c("chr1", "chr2", "chr3", "chr4"))

That outputs a graphics object directly. It doesn't save it to any variable. However, I've made some progress:

x <- recordPlot(load=NULL, attach=NULL)``

recordPlot() will take whatever has been output to the graphics viewport and save it. Then I can capture that using as_grob:

aGrob <- as_grob(x)

That gets me pretty far. I can now save karyoplots to a list, and I can call them and save them as pngs. What I'd like to do is rotate them and arrange them in a grid, though.

I can rotate them with:

aGrob <- editGrob(aGrob, vp=viewport(angle=-90))    

However, I still can't plot them in a grid. Currently, when I try, I get a lot of errors and plots are missing. It might not be obvious, but in the image below, there is a big empty space where Chr1 should be.

grid.arrange(plotList[[1]], nrow = 1)

will produce the following: enter image description here

but

grid.arrange(plotList[[1]], plotList[[2]], nrow = 1)

produces: enter image description here

It reports the following errors:

Warning messages: 1: In grid.Call.graphics(C_setviewport, vp, TRUE) : cannot clip to rotated viewport

ADD COMMENT

Login before adding your answer.

Traffic: 1712 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6