2015-05-29 23 views
3

4繪出我有麻煩指定以便粘在2×2情節四個圖(2列,2行)中ggplot網格佈局尺寸。我具有R代碼,粘在第一2繪出一排並且如在下面的圖中概述在單獨的行中的第三和第四圖(共3行)。 output of the R code。使用該簡易R代碼示例生成此情節:在ggplot指定grid.layout粘在2×2的情節

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個地塊劃分成一排(類似於前兩個地塊),因此我有一個可接受的出版小地塊。

+0

您SSEM有錯誤的行和列表示:嘗試改變有關行'打印(plotobj3,VP = vplayout(3:4,1:2))'和'打印(plotobj4,VP = vplayout(3:4,3:4))' – user20650

+4

可能是更容易使用'從'gridExtra'包grid.arrange'。 (例如'grid.arrange(plotobj1,plotobj2,plotobj3,plotobj4)' – user20650

+0

檢查了這一點也許使用額外的包可能是無痛的解決方案。http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ – SabDeM

回答

1

雖然你也可以使用cookbook-r.com上面提到的grid.arrangemultiplot函數,你的代碼也可以工作。但是,你難以讓自己指定一個4×4格,而所有你需要的是一個2由2.順便說一句:你不需要作空對象第一,所以我已刪除了那些線。

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