Closed EstherHerbert closed 2 years ago
I forgot to include a simple example of what I'd like to eventually achieve:
Hi,
There are two ways to combine two or more consort plots:
# Method 1: gridExtra package
grid.arrange(grobTree(p1), grobTree(p2), ncol = 2)
# Method 2: grid package
grid.newpage()
gf <- frameGrob(layout = grid.layout(1, 2))
gf <- packGrob(gf, grobTree(p1), row = 1, col = 1, dynamic = TRUE)
gf <- packGrob(gf, grobTree(p2), row = 1, col = 2, dynamic = TRUE)
grid.draw(gf)
# Connecting box
# List the objects
grid.ls(gf)
#> GRID.frame.107
#> GRID.cellGrob.109
#> GRID.gTree.108
#> vertbox.1
#> sidebox.2
#> vertbox.1-sidebox.2
#> vertbox.3
#> vertbox.1-vertbox.3
#> GRID.cellGrob.111
#> GRID.gTree.110
#> vertbox.4
#> sidebox.5
#> vertbox.4-sidebox.5
#> vertbox.6
#> vertbox.4-vertbox.6
# Draw arrow
# Adjust 0.5 below to your need, 0.5 is middle of Y axis
grid.segments(grobX(p1[[4]], 180) , .5,
grobX(p2[[4]], 0), .5,
arrow = arrow(length=unit(3, "mm"), type="closed"),
gp=gpar(fill="black"))
# Or from the package function
ln <- connect_box(p1[[4]], p2[[4]], connect = "lr")
grid.draw(ln)
Created on 2022-03-15 by the reprex package (v2.0.1)
As you can see the output is not perfect, but you can change the x and y axis to meet your need. Also, the trick is finding the correct vertical box you want to connect. The gird.ls()
function should be able to show you the names of the plot components. As you can see from the output, vertbox
is the name of the vertical box and vertbox.4-vertbox.6
is the connection arrow from box 4 to box 6. Here, you want to connect vertbox.3
with vertbox.6
. You can loop through name, as you should be able to see the name of the 4th element of p1
. This is same for the p2
p1[[4]]$name
#> [1] "vertbox.3"
Hope this solve your problem.
That's really helpful thank you!
Hello,
Thanks for the package! I'd like to be able to display two consort diagrams next to each other, specifically for situations like case-control designs where the data has come from two source populations. However, I'm struggling to find a way to do this successfully. I've tried using grid.arrange from gridExtra but that doesn't work:
Created on 2022-03-15 by the reprex package (v2.0.1)
Are there any plans to include capability for these situations into the package?