r - 使用 coord_fixed 进行多图布局

标签 r ggplot2

您能否对齐示例中的图,以便它们按顶部对齐? 这只是一个问题,因为我使用 coord_fixed (但我需要)。

我的两个(小)文本标签按其应有的方式显示,但这两个图放置在单元格的垂直中心。

我已经查看并用谷歌搜索过,但我放弃了......

进行编辑以反射(reflect)下面讨论的问题/解决方案:

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")

enter image description here

最佳答案

试试这个(面板对齐,但不包括小面条)

,

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)

关于r - 使用 coord_fixed 进行多图布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43760561/

相关文章:

r - 组别比例表

Rcpp如何将IntegerVector转换为NumericVector

R:如何找到数据帧两行元素的交集?

r - 无法更改 ggplot2 直方图上的颜色

r - 在 ggplot2 中与刻面一起创建华夫饼图

r - 为什么 withCallingHandlers 仍然停止执行?

r - R 中使用 Plotly 绘制多个 y 轴图表

r - 仅在指定因素之间连接 geom_line

r - 如何不显示 ggplot 轴上的所有标签?

r - 如何围绕模型估计手动绘制 SE/CI