相关疑难解决方法(0)

使用par()或layout()函数组合ggplot2(不在单个图中)的图?

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

假设我想绘制散点图的ggplot和直方图的ggplot.我希望将这两个图组合在一起(不是在单个画面中).适用吗?

我在R中使用简单的绘图尝试了它,而没有使用ggplot函数.它确实有效.

以下是来自Quick-R的示例链接: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)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用ggplot并组合情节时,我没有得到输出.

layout r par ggplot2

24
推荐指数
3
解决办法
3万
查看次数

ggplot2:使用grid.arrange()作为do.call()的参数定义绘图布局

我想获得一个不平衡的情节网格,如

require(ggplot2)
require(gridExtra)

df <- data.frame(value1 = rnorm(200),
                 value2 = rnorm(200),
                 value3 = rnorm(200),
                 value4 = rnorm(200))

p1 <- ggplot(df) + geom_density(aes(x=value1))
p2 <- ggplot(df) + geom_density(aes(x=value2))
p3 <- ggplot(df) + geom_density(aes(x=value3))
p4 <- ggplot(df) + geom_density(aes(x=value4))

grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是使用了一个功能

myplot <- function(i){
  p <- ggplot(df) + geom_density(aes_string(x=i))
  return(p)
}
Run Code Online (Sandbox Code Playgroud)

和一个lapply电话

p <- lapply(c("value1","value2","value3","value4"), myplot)
do.call(grid.arrange, c(p))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,grid.arrange将图分布在2×2矩阵中.但是我希望得到一个不平衡的布局

grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gridextra

14
推荐指数
1
解决办法
3万
查看次数

标签 统计

ggplot2 ×2

r ×2

gridextra ×1

layout ×1

par ×1