2018-10-18
閱讀量:
1339
ggplot2來實(shí)現(xiàn)一頁多圖
通過構(gòu)建multiplot函數(shù),能夠很容易地做到一頁多圖,該函數(shù)的具體定義附在末尾,如果它并不能完全滿足你的需求,可以復(fù)制它并在它的基礎(chǔ)上進(jìn)行修改。
首先,構(gòu)建一系列圖像,但不直接去渲染它們,圖像的具體細(xì)節(jié)并不重要,我們只需要將這些圖像對象全部存儲為變量。
library(ggplot2)
? ? # 下面的例子用到了ggplot2包中自帶的示例數(shù)據(jù)集ChickWeight
? ? # 首先創(chuàng)建圖像,第一幅圖像——折線圖
? ? p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
? ?? ???geom_line() +
? ?? ???ggtitle("Growth curve for individual chicks")
? ? # 第二幅圖像——密度分布圖
? ? p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
? ?? ???geom_point(alpha=.3) +
? ?? ???geom_smooth(alpha=.2, size=1) +
? ?? ???ggtitle("Fitted growth curve per diet")
? ? # 第三幅圖像——帶擬合線的散點(diǎn)圖
? ? p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
? ?? ???geom_density() +
? ?? ???ggtitle("Final weight, by diet")
? ? # 第四幅圖像——分面直方圖
? ? p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
? ?? ???geom_histogram(colour="black", binwidth=50) +
? ?? ???facet_grid(Diet ~ .) +
? ?? ???ggtitle("Final weight, by diet") +
? ?? ???theme(legend.position="none")? ?? ???# 無圖例(在這幅圖中,圖例顯得太冗余了)
接下來,我們可以用multiplot函數(shù)對創(chuàng)建的圖像進(jìn)行渲染,將它們展示為兩行。
multiplot(p1, p2, p3, p4, cols=2)
? ? #> 載入需要grid包
? ? #> geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
下面是multiplot函數(shù)的具體定義,你可以把任意數(shù)量的圖像名作為其參數(shù),或者構(gòu)建一個(gè)圖像列表作為函數(shù)中的plotlist。
# Multiple plot function
? ? #
? ? # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
? ? # - cols:? ?Number of columns in layout
? ? # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
? ? #
? ? # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
? ? # then plot 1 will go in the upper left, 2 will go in the upper right, and
? ? # 3 will go all the way across the bottom.
? ? #
? ? multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
? ?? ?library(grid)
? ?? ?# Make a list from the ... arguments and plotlist
? ?? ?plots <- c(list(...), plotlist)
? ?? ?numPlots = length(plots)
? ?? ?# If layout is NULL, then use 'cols' to determine layout
? ?? ?if (is.null(layout)) {
? ?? ???# Make the panel
? ?? ???# ncol: Number of columns of plots
? ?? ???# nrow: Number of rows needed, calculated from # of cols
? ?? ???layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
? ?? ?? ?? ?? ?? ?? ?? ?ncol = cols, nrow = ceiling(numPlots/cols))
? ?? ?}
? ???if (numPlots==1) {
? ?? ???print(plots[[1]])
? ?? ?} else {
? ?? ???# Set up the page
? ?? ???grid.newpage()
? ?? ???pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
? ?? ???# Make each plot, in the correct location
? ?? ???for (i in 1:numPlots) {
? ?? ?? ? # Get the i,j matrix positions of the regions that contain this subplot
? ?? ?? ? matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
? ?? ?? ? print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?layout.pos.col = matchidx$col))
? ?? ???}
? ?? ?}
? ? }






評論(0)


暫無數(shù)據(jù)
CDA考試動態(tài)
CDA報(bào)考指南
推薦帖子
0條評論
0條評論
0條評論