Layout multipoint files with cop_fixed
Is it possible to align the plots in the example so they are vertex aligned? This is ONLY a problem because I am using coord_fixed (but I need to).
My two (small) text labels display as they should, but these two graphics are placed in the vertical center of the cell.
I have looked and searched Google and I give up ...
Edited to reflect the problem / solution discussed below:
library(ggplot2)
library(grid)
library(gridExtra)
plot1b = ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(ratio=5)
plot2b = ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(ratio=5) +
facet_wrap( ~ cyl, ncol=2)
g1 = ggplotGrob(plot1b + theme(plot.margin = unit(c(0,0,0,0),'cm'), plot.background = element_rect(fill="red")))
g2 = ggplotGrob(plot2b + theme(plot.margin = unit(c(0,0,0,0),'cm'), plot.background = element_rect(fill="red")))
t1 = arrangeGrob(g1,left = textGrob("A", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
t2 = arrangeGrob(g2,left = textGrob("B", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
f1 = arrangeGrob(t1,t2, ncol=2)
g1 = ggplotGrob(plot1b + theme(plot.margin = unit(c(0,0,8,0),'cm'), plot.background = element_rect(fill="red")))
g2 = ggplotGrob(plot2b + theme(plot.margin = unit(c(0,0,8,0),'cm'), plot.background = element_rect(fill="red")))
t1 = arrangeGrob(g1,left = textGrob("A", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
t2 = arrangeGrob(g2,left = textGrob("B", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
f2 = arrangeGrob(t1,t2, ncol=2)
f3 =arrangeGrob(f1,f2,ncol=2)
plot(f3)
ggsave(plot=f3, file="plot.png")
+3
source to share
2 answers
try this (panels are aligned, but not including facet stripes)
,
library(ggplot2)
library(grid)
library(gridExtra)
p1 = ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + coord_fixed(ratio=1)+ theme(plot.background = element_rect(colour = "black"))
p2 = ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + coord_fixed(ratio=1) +
facet_wrap( ~ cyl, ncol=2) + theme(plot.background = element_rect(colour = "black"))
g1 = ggplotGrob(p1 )
g2 = ggplotGrob(p2)
g <- egg::ggarrange(p1, p2,ncol=2, draw = FALSE)
g <- gtable::gtable_add_grob(g, list(textGrob("A"), textGrob("B")), t = 1, l=c(1,4), z=-Inf)
grid.newpage()
grid.draw(g)
+1
source to share
Hope I understood your question. I added a large bottom edge at each section to push them towards the top. (note that it is not actually squashed as shown in this image)
library(ggplot2)
library(grid)
library(gridExtra)
plot1b = ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + coord_fixed(ratio=1)+
theme(plot.margin = unit(c(0,0,12,0),'cm'))
plot2b = ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + coord_fixed(ratio=1) +
facet_wrap( ~ cyl, ncol=2)+
theme(plot.margin = unit(c(0,0,12,0),'cm'))
g1 = ggplotGrob(plot1b)
g2 = ggplotGrob(plot2b)
t1 = arrangeGrob(g1,left = textGrob("A", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
t2 = arrangeGrob(g2,left = textGrob("B", y=1, vjust=1, gp=gpar(fontsize=7, fontface="bold")))
f1 = arrangeGrob(t1,t2, ncol=2)
plot(f1)
0
source to share