完美地对齐几个图

Ale*_*lex 6 r ggplot2

我的目标是一个复合图,它结合了散点图和2个图来进行密度估算.我面临的问题是由于密度图的缺失轴标记和散点图的图例,密度图未能与散点图正确对齐.它可以通过播放arround来调整plot.margin.但是,这不是一个更好的解决方案,因为如果对图形进行更改,我将不得不一遍又一遍地进行调整.有没有办法以某种方式定位所有绘图,以便实际绘图面板完美对齐?

在此输入图像描述

我尽量保持代码尽可能小,但为了重现问题,它仍然是相当多的.

library(ggplot2)
library(gridExtra)

df <- data.frame(y     = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), 
                 x     = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), 
                 group = factor(c(rep(0, 50), rep(1,50))))


empty <- ggplot() + 
              geom_point(aes(1,1), colour="white") +
              theme(                              
                plot.background = element_blank(), 
                panel.grid.major = element_blank(), 
                panel.grid.minor = element_blank(), 
                panel.border = element_blank(), 
                panel.background = element_blank(),
                axis.title.x = element_blank(),
                axis.title.y = element_blank(),
                axis.text.x = element_blank(),
                axis.text.y = element_blank(),
                axis.ticks = element_blank()
              )


scatter <-  ggplot(df, aes(x = x, y = y, color = group)) + 
                geom_point() +
                theme(legend.position = "bottom")

top_plot <- ggplot(df, aes(x = y)) + 
                geom_density(alpha=.5, mapping = aes(fill = group)) + 
                theme(legend.position = "none") +
                theme(axis.title.y = element_blank(),
                      axis.title.x = element_blank(),
                      axis.text.y=element_blank(),
                      axis.text.x=element_blank(),
                      axis.ticks=element_blank() )

right_plot <- ggplot(df, aes(x = x)) + 
                geom_density(alpha=.5, mapping = aes(fill = group)) + 
                coord_flip() + theme(legend.position = "none") +
                theme(axis.title.y = element_blank(),
                      axis.title.x = element_blank(),
                      axis.text.y  = element_blank(),
                      axis.text.x=element_blank(),
                      axis.ticks=element_blank())

grid.arrange(top_plot, empty, scatter, right_plot, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
Run Code Online (Sandbox Code Playgroud)

bap*_*ste 5

另外一个选项,

library(egg) 
ggarrange(top_plot, empty, scatter, right_plot, 
          ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


use*_*650 1

使用Align ggplot2plotsvertical的答案通过添加到 gtable 来对齐绘图(很可能使这变得复杂!!)

library(ggplot2)
library(gtable)
library(grid)
Run Code Online (Sandbox Code Playgroud)

您的数据和绘图

set.seed(1)
df <- data.frame(y     = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), 
                 x     = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), 
                 group = factor(c(rep(0, 50), rep(1,50))))

scatter <-  ggplot(df, aes(x = x, y = y, color = group)) + 
                geom_point() +  theme(legend.position = "bottom")

top_plot <- ggplot(df, aes(x = y)) + 
                geom_density(alpha=.5, mapping = aes(fill = group))+
               theme(legend.position = "none") 

right_plot <- ggplot(df, aes(x = x)) + 
                geom_density(alpha=.5, mapping = aes(fill = group)) + 
                coord_flip() + theme(legend.position = "none") 
Run Code Online (Sandbox Code Playgroud)

使用 Bapistes 答案中的想法

g <- ggplotGrob(scatter)

g <- gtable_add_cols(g, unit(0.2,"npc"))    
g <- gtable_add_grob(g, ggplotGrob(right_plot)$grobs[[4]], t = 2, l=ncol(g), b=3, r=ncol(g))

g <- gtable_add_rows(g, unit(0.2,"npc"), 0)
g <- gtable_add_grob(g, ggplotGrob(top_plot)$grobs[[4]], t = 1, l=4, b=1, r=4)

grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

哪个产生

在此输入图像描述

我曾经手动ggplotGrob(right_plot)$grobs[[4]]选择panelgrob,但当然你可以自动执行此操作

还有其他选择:Scatterplot with margin histograms in ggplot2