ggplot2 - 多个绘图缩放 [英] ggplot2 - multiple plots scaling

查看:512
本文介绍了ggplot2 - 多个绘图缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2生成多个网格图。所以我想用x轴以下的附加boxplot生成分布图,以及对于不同的组和变量,如下所示:



CODE:我试着用下面的代码来做到这一点:

  library(ggplot2)
require(grid)

x = rbind(data.frame(D1 = rnorm(1000),Name =titi,ID = c(1:1000)) ,
data.frame(D1 = rnorm(1000)+1,Name =toto,ID = c(1:1000)))

space = 1
suite = 1
p1 = ggplot(x,aes(x = D1,color = Name,fill = Name))+
geom_histogram(aes(y = .. density ..),alpha = 0.35,color =
geom_density(alpha = .2,size = 1)+
theme_minimal()+
labs = adjustcolor(white,0),position =identity,binwidth = 0.05)+
(x = NULL,y =Density)+
theme(legend.position =top,
legend.title = element_blank())+
scale_fill_manual(values = c( gray30,royalblue1))+
scale_color_manual(values = c(gray30,royalblue1))

p2 = ggplot(x,aes(x = factor(Name),y = D1,fill = factor(Name) = x因子(名称)))+
geom_boxplot(alpha = 0.2)+
theme_minimal()+
coord_flip()+
实验室(x = NULL,y = NULL)+
theme(legend.position =none,
axis.text.y = element_blank(),
axis.text.x = element_blank(),
panel.grid。 minor.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major。 y = element_blank())+
scale_fill_manual(values = c(gray30,royalblue1))+
scale_color_manual(values = c(gray30,royalblue1))

grid.newpage()
pushViewport(viewport(layout = grid.layout(5,1)))
define_region< - function(row,col){
viewport (p2,vp = define_region(1:4,1))
print(p2,vp = define_region(5,1))

结果:





问题:在我的搜索过程中,我观察到密度分布图与盒图之间的比例不一样(问题1)。我还没有找到解决方案,将这两个图形绘制成网格(我迷路了)。

解决方案使用 cowplot 包会变得更容易一些。但是,我们应该适当地设置x轴范围以确保它们对于两个图都是相同的。这是因为密度图比纯数据图自然要宽一些,因此 p1 的坐标轴会稍宽一些。当轴被固定时,我们可以排列和对齐它们(轴文本和边距将不再重要)。

  library(cowplot) 
comb < - plot_grid(p1 + xlim(-5,5),
p2 + ylim(-5,5),#因为coord_flip()使用ylim代替p2()
align = 'v',rel_heights = c(4,1),nrow = 2)



同样,我们可以安排多个组合图:

  plot_grid(comb,comb,comb,comb)


I tried to generate multiple grid plots with ggplot2. So I would like to generate distribution plot with additional boxplot below x-axis and that for different groups and variables like that:

CODE: I tried to do that with the following code :

library(ggplot2)
require(grid)

x=rbind(data.frame(D1=rnorm(1000),Name="titi",ID=c(1:1000)),
    data.frame(D1=rnorm(1000)+1,Name="toto",ID=c(1:1000)))

space=1
suite=1
p1=ggplot(x, aes(x=D1, color=Name, fill=Name)) + 
geom_histogram(aes(y=..density..),alpha=0.35,color=adjustcolor("white",0),position="identity",binwidth = 0.05)+
  geom_density(alpha=.2,size=1)+
  theme_minimal()+
  labs(x=NULL,y="Density")+
  theme(legend.position = "top",
        legend.title = element_blank())+
  scale_fill_manual(values=c("gray30","royalblue1"))+
  scale_color_manual(values=c("gray30","royalblue1"))

p2=ggplot(x, aes(x=factor(Name), y=D1,fill=factor(Name),color=factor(Name)))+
  geom_boxplot(alpha=0.2)+
  theme_minimal()+
  coord_flip()+
  labs(x=NULL,y=NULL)+
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_blank())+
  scale_fill_manual(values=c("gray30","royalblue1"))+
  scale_color_manual(values=c("gray30","royalblue1"))

grid.newpage()
pushViewport(viewport(layout=grid.layout(5,1)))
define_region <- function(row, col){
  viewport(layout.pos.row = row, layout.pos.col = col)
}
print(p1, vp=define_region(1:4,1))
print(p2, vp=define_region(5,1))

RESULT:

QUESTION: During my search I observed that scale between density distribution plot and boxplot are not the same (problem 1). I haven't found solution to plot these two graph in grid (I'm lost).

解决方案

With the cowplot package this becomes a bit easier. However, we should properly set the x-axis range to ensure they are the same for both plots. This is because the density plots are naturally a bit wider than pure data plots, and the axis for p1 will therefore be a bit wider. When the axes are fixed we can arrange and align them (axis text and margins will no longer matter).

library(cowplot)
comb <- plot_grid(p1 + xlim(-5, 5), 
                  p2 + ylim(-5, 5),               # use ylim for p2 because of coord_flip()
                  align = 'v', rel_heights = c(4, 1), nrow = 2)

Similarly we can arrange multiples of the combination plots:

plot_grid(comb, comb, comb, comb)

这篇关于ggplot2 - 多个绘图缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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