首页 文章

在ggplot中指定grid.layout以在2x2图中粘贴4个图

提问于
浏览
3

我在ggplot中指定网格布局尺寸时遇到问题,以便在2x2图(2列,2行)中粘贴四个图 . 我有一个R代码,它将前两个图分成一行,第三和第四个图分别在不同的行中(共3行),如下图所示 .
output of the R code
. 这个图是使用这个简单的R代码示例生成的:

setwd("...")
library(ggplot2)
library(grid)
x1 <- c(seq(1,20,1))
y1 <- c(seq(50,69,1))

df <- data.frame(x1,y1)
df$DOSE[df$x1<= 10] <- "50 mg"
df$DOSE[df$x1 > 10] <- "100 mg"


filename <- "test_plot.png"
png(filename, width=700, height=900, pointsize=14)

#4 ggplot2 graphs in a grid layout
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(4,4)))

#Plot 1
plotobj1 <- NULL
plotobj1 <-  ggplot(data=df)
plotobj1 <- plotobj1 + geom_point(aes(x=x1, y=y1,colour=DOSE), shape=1, size=3)
plotobj1  <-  plotobj1 + theme(legend.position="none")
print(plotobj1, vp=vplayout(1:2,1:2))
#Plot 2
plotobj2 <- NULL
plotobj2 <-  ggplot(df)
plotobj2 <- plotobj2 + geom_point(aes(x=x1, y=y1,colour=DOSE), shape=1, size=3)
plotobj2  <-  plotobj2 + theme(legend.position="none")
print(plotobj2, vp=vplayout(1:2,3:4))
#Plot 3 
plotobj3 <- NULL
plotobj3 <-  ggplot(df)
plotobj3 <- plotobj3 + geom_point(aes(x=x1, y=y1, colour=DOSE), shape=1, size=3)
plotobj3  <-  plotobj3 + scale_colour_brewer(name="Dose", palette="Set1")
print(plotobj3, vp=vplayout(3,2:4)) 
#Plot 4   CWRES vs PRED
plotobj4 <- NULL
plotobj4 <-  ggplot(df)
plotobj4 <- plotobj4 + geom_point(aes(x=x1, y=y1, colour=DOSE), shape=1, size=3)
plotobj4  <-  plotobj4 + scale_colour_brewer(name="Dose", palette="Set1")
print(plotobj4, vp=vplayout(4,2:4))

dev.off()

我无法修改尺寸和绘制位置 . 我想将第3和第4个图分成一行(类似于前两个图),所以我有一个较小的图可以接受发表 .

1 回答

  • 1

    虽然您也可以使用cookbook-r.com上面提到的 grid.arrangemultiplot 函数,但您的代码也可以使用 . 但是,您需要先创建一个NULL对象,所以我删除了这些行 .

    library(ggplot2)
    library(grid)
    x1 <- c(seq(1,20,1))
    y1 <- c(seq(50,69,1))
    df <- data.frame(x1,y1)
    df$DOSE[df$x1<= 10] <- "50 mg"
    df$DOSE[df$x1 > 10] <- "100 mg"
    
    filename <- "test_plot.png"
    png(filename, width=700, height=900, pointsize=14)
    
    #4 ggplot2 graphs in a grid layout
    vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(2,2)))
    
    #Plot 1
    plotobj1 <-  ggplot(data=df)
    plotobj1 <- plotobj1 + geom_point(aes(x=x1, y=y1,colour=DOSE), shape=1, size=3)
    plotobj1  <-  plotobj1 + theme(legend.position="none")
    print(plotobj1, vp=vplayout(1,1))
    #Plot 2
    plotobj2 <-  ggplot(df)
    plotobj2 <- plotobj2 + geom_point(aes(x=x1, y=y1,colour=DOSE), shape=1, size=3)
    plotobj2  <-  plotobj2 + theme(legend.position="none")
    print(plotobj2, vp=vplayout(1,2))
    #Plot 3 
    plotobj3 <-  ggplot(df)
    plotobj3 <- plotobj3 + geom_point(aes(x=x1, y=y1, colour=DOSE), shape=1, size=3)
    plotobj3  <-  plotobj3 + scale_colour_brewer(name="Dose", palette="Set1")
    print(plotobj3, vp=vplayout(2,1)) 
    #Plot 4   CWRES vs PRED
    plotobj4 <-  ggplot(df)
    plotobj4 <- plotobj4 + geom_point(aes(x=x1, y=y1, colour=DOSE), shape=1, size=3)
    plotobj4  <-  plotobj4 + scale_colour_brewer(name="Dose", palette="Set1")
    print(plotobj4, vp=vplayout(2,2))
    
    dev.off()
    

    A 2x2 layout

相关问题