Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate chordDiagram() with ggdraw function to combine ggplot objects #339

Open
zh-zhang1984 opened this issue Oct 6, 2022 · 4 comments

Comments

@zh-zhang1984
Copy link

Suppose I have several ggplot2 objects and I want to combine all panel figures in a large figure; other figures are generated with ggplot2. Are there any efficient method to do so; I found the chordDiagram() returned a data.frame object.

@jokergoo
Copy link
Owner

jokergoo commented Oct 6, 2022

circlize is implemented with the base graphics. To be combined it with the ggplot2 plots, you need to use gridBase package.

@rbutleriii
Copy link

@zh-zhang1984 it turns out you can pass it as a formula with patchwork:

library(patchwork)
library(ggplot2)
library(circlize)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
mat = matrix(1:9, 3)
rownames(mat) = letters[1:3]
colnames(mat) = LETTERS[1:3]
p1 + wrap_elements(full = ~ chordDiagram(mat))

However, @jokergoo , I have a problem if I want to rotate the text per the book:

p2 = ~ chordDiagram(mat, annotationTrack = "grid",
                  preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(mat))))),
                  directional = 1, direction.type = c("diffHeight", "arrows"),
                  link.arr.type = "big.arrow")
title("I want a title")
# we go back to the first track and customize sector labels
circos.track(track.index = 1, panel.fun = function(x, y) {
    circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index, 
        facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
}, bg.border = NA) # here set bg.border to NA is important
p1 + wrap_elements(full=p2)

It will not accept the additional base graphics, nor the annotation track:

Error in title("I want a title") : plot.new has not been called yet
Error in polygon(polar2Cartesian(d2), ...) :
  plot.new has not been called yet

@jokergoo
Copy link
Owner

jokergoo commented Feb 6, 2023

@rbutleriii if you do so, wrap all the base graphics code into a code block:

p2 = ~ {
chordDiagram(mat, annotationTrack = "grid",
                  preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(mat))))),
                  directional = 1, direction.type = c("diffHeight", "arrows"),
                  link.arr.type = "big.arrow")
title("I want a title")
# we go back to the first track and customize sector labels
circos.track(track.index = 1, panel.fun = function(x, y) {
    circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index, 
        facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
}, bg.border = NA) # here set bg.border to NA is important
}

I haven't used patchwork, but I guess it should work.

@rbutleriii
Copy link

Excellent! Yes that does work to wrap the whole thing:
test

Also, the link to the grid/cowplot method:
jokergoo/ComplexHeatmap#735

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants