使用par()或layout()函数组合ggplot2的图形(不是在一个图中)? [英] Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function?

查看:1016
本文介绍了使用par()或layout()函数组合ggplot2的图形(不是在一个图中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑使用par()或layout()函数来组合ggplots。是否有可能使用这些函数?



假设我想绘制散点图的ggplot和直方图的ggplot。我想结合这两个情节(不在一个单一的地块)。它是否适用?



我在R中使用简单的绘图来测试它,而不使用ggplot函数。它实际上是有效的。



以下是Quick-R,Link的一个示例: http://www.statmethods.net/advgraphs/layout.html

 #4个数字排列在2行和2列
附加(mtcars)
par(mfrow = c(2,2))
plot(wt,mpg,main =wt对mpg的散点图)
plot(wt,disp,main =wt vs disp的散点图)
hist(wt,main =直方图wt)
boxplot(wt,main =Boxplot of wt )

#行1中的一个数字和行2中的两个数字
attach(mtcars)
layout(矩阵(c(1,1,2,3),2, 2,byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

但是,当我尝试使用ggplot,并结合图,我没有得到输出。 / div>

  library(ggplot2)
library(grid)


vplayout< - function(x,y)viewport (layout.pos.row = x,layout.pos.col = y)


plot1< - qplo t(mtcars,x = wt,y = mpg,geom =point,main =wt对mpg的散点图)
plot2 <-qplot(mtcars,x = wt,y = disp,geom =point,main =wt对disp的散点图)
plot3 <-qplot(wt,data = mtcars)
plot4 <-qplot(wt,mpg,data = mtcars,geom =boxplot)
plot5 <-qplot(wt,data = mtcars)
plot6 <-qplot(mpg,data = mtcars)
plot7 <-qplot(disp,data = mtcars)

#排列成2行2列的数字
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))
print(plot1,vp = vplayout(1,1))
print(plot2,vp = vplayout(1,2))
print(plot3,vp = vplayout(2,1) )
print(plot4,vp = vplayout(2,2))


#行1中的一个数字和行2中的两个数字
grid.newpage( )
pushViewport(viewport(layout = grid.layout(2,2)))
print(plot5,vp = vplayout(1,1:2))
print(plot6,vp = vplayout(2,1))
print(plot7,vp = vplayout(2,2))


I've been thinking of using par() or layout() functions for combining ggplots. Will it be possible to use those functions?

Say I want to plot ggplot for scatterplot and ggplot for histogram. And I want to combine the two plots (NOT IN A SINGLE PLOT). Is it applicable?

I tried it with simple plotting in R, without using the ggplot functions. And it works actually.

Here's a sample from Quick-R, Link: http://www.statmethods.net/advgraphs/layout.html

# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")

# One figure in row 1 and two figures in row 2
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

But when I try to use ggplot, and combine the plot, I don't get an output.

解决方案

library(ggplot2)
library(grid)


vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)


plot1 <- qplot(mtcars,x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg")
plot2 <- qplot(mtcars,x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp")
plot3 <- qplot(wt,data=mtcars)
plot4 <- qplot(wt,mpg,data=mtcars,geom="boxplot")
plot5 <- qplot(wt,data=mtcars)
plot6 <- qplot(mpg,data=mtcars)
plot7 <- qplot(disp,data=mtcars)

# 4 figures arranged in 2 rows and 2 columns
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))
print(plot1, vp = vplayout(1, 1))
print(plot2, vp = vplayout(1, 2))
print(plot3, vp = vplayout(2, 1))
print(plot4, vp = vplayout(2, 2))


# One figure in row 1 and two figures in row 2
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))
print(plot5, vp = vplayout(1, 1:2))
print(plot6, vp = vplayout(2, 1))
print(plot7, vp = vplayout(2, 2))

这篇关于使用par()或layout()函数组合ggplot2的图形(不是在一个图中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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