# r - x軸刻度 - 繪圖參數type已過時

## R-將多重繪圖保存到文件 (2)

``````png("chickweight.png")
multiplot(p1, p2, p3, p4, cols=2)
dev.off()
``````

``````##define multiplot function
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)

# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)

numPlots = length(plots)

# Make the panel
plotCols = cols                          # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)

# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}

}

## define subplots (short example here, I specified some more aesthetics in my script)
plot1a <- qplot(variable1,variable2,data=Mydataframe1)
plot1b <- qplot(variable1,variable3,data=Mydataframe1)
plot1c <- qplot(variable1,variable2,data=Mydataframe2)
plot1d <- qplot(variable1,variable3,data=Mydataframe2)

## plot in one frame
Myplot <- multiplot(plot1a,plot1b,plot1c,plot1d, cols=2)
``````

1. 使用ggsave

``````ggsave(filename = "D:/R/plots/Myplots.jpg")
``````

這導致僅保存子圖1d。

2. 使用jpeg（），print（）和dev.off（）

``````jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE)
print(Myplot)
dev.off()
``````

這導致完全白色的圖像（只是我假設的背景）。 print（Myplot）返回NULL。

``````jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE)
multiplot(plot1a,plot1b,plot1c,plot1d, cols=2)
dev.off()
``````