完美对齐几个地块 [英] Perfectly align several plots

查看:128
本文介绍了完美对齐几个地块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我试着将代码保持为最小尽可能,但为了重现这个问题,它仍然相当多。

  library(ggplot2)
library(gridExtra)

df< - data。帧(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))))


空< - ggplot()+
geom_point(aes(1,1),color =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())
$ b $ grid.arrange(top_plot,empty,scatter,right_plot, ncol = 2,nrow = 2,widths = c(4,1),heights = c(1,4))


解决方案

使用



我用 ggplotGrob(right_plot)$ grobs [[4]] 手动选择面板 grob,但当然也可以自动执行此操作。

还有其他选择: ggplot2中有边缘直方图的散点图


My aim is a compounded plot which combines a scatterplot and 2 plots for density estimates. The problem I'm facing is that the density plots do not align correctly with the scatter plot due to the missing axes labeling of the density plots and the legend of the scatter plot. It could be adjusted by playing arround with plot.margin. However, this would not be a preferable solution since I would have to adjust it over and over again if changes to the plots are made. Is there a way to position all plots in a way so that the actual plotting panels align perfectly?

I tried to keep the code as minimal as possible but in order to reproduce the problem it is still quite a lot.

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

解决方案

Using the answer from Align ggplot2 plots vertically to align the plot by adding to the gtable (most likely over complicating this!!)

library(ggplot2)
library(gtable)
library(grid)

Your data and plots

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

Use the idea from Bapistes answer

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)

Which produces

I used ggplotGrob(right_plot)$grobs[[4]] to select the panel grob manually, but of course you could automate this

There are also other alternatives: Scatterplot with marginal histograms in ggplot2

这篇关于完美对齐几个地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆