2012-12-09 53 views
3

在r的multiplot不工作,我已經準備了多個ggplot2圖中,保存到變量是這樣的:R:因爲錯誤UseMethod(「getModelInfo」,模型)

flashplot <- ggplot(flash, aes(x=flash$Year, y=flash$Proc)) 
+ stat_smooth(method="lm", formula = "flash$Proc ~ poly(flash$Year, 2, raw=TRUE)", n=14, level=0.9) 
+ geom_point(shape=19) + ylab("Percentage of total existing versions") 
+ xlab("Years of Existence") + scale_x_continuous(breaks = c(0,2,4,6,8,10,12,14,16,18,20)) 

繪製一個單一的數字作品如預期的那樣,但我想使用多點劃分4個數字(2乘2),如in the R cookbook所述。

當我得到一個錯誤,我試過的例子從食譜,我thould會的工作,但它給了我同樣的錯誤:在Windows 7下

> multiplot(p1, p2, p3, p4, cols=2) 
Error in UseMethod("getModelInfo", model) : 
    no applicable method for 'getModelInfo' applied to an object of class "c('gg', 'ggplot')" 

我,使用R 2.15.2,和ggplot2coefplot最新的軟件包:

> sessionInfo() 
R version 2.15.2 (2012-10-26) 
Platform: i386-w64-mingw32/i386 (32-bit) 

locale: 
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252 
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C     
[5] LC_TIME=German_Austria.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] coefplot_1.1.8 ggplot2_0.9.3 

loaded via a namespace (and not attached): 
[1] colorspace_1.2-0 dichromat_1.2-4 digest_0.6.0  grid_2.15.2  
[5] gtable_0.1.2  labeling_0.1  MASS_7.3-22  munsell_0.4  
[9] plyr_1.8   proto_0.3-9.2  RColorBrewer_1.0-5 reshape2_1.2.2  
[13] scales_0.2.3  stringr_0.6.2  useful_1.1.6  

任何提示,以幫助我解決我的問題將高度讚賞。我錯過了什麼嗎?我已經在網上搜索,但一無所獲有用

- 按照要求,這裏是示例數據:

library(ggplot2) 

# This example uses the ChickWeight dataset, which comes with ggplot2 
# First plot 
p1 <- 
    ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) + 
    geom_line() + 
    ggtitle("Growth curve for individual chicks") 

# Second plot 
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") 

# Third plot 
p3 <- 
    ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) + 
    geom_density() + 
    ggtitle("Final weight, by diet") 

# Fourth plot 
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")  # No legend (redundant in this graph) 

multiplot(p1, p2, p3, p4, cols=2) 
+0

你能提供的數據或製造假數據,使我們有一個重複的例子,我們可以複製,粘貼和試驗? – SlowLearner

+0

由於R食譜的例子不起作用,我認爲這對我準備好的圖不是問題。我已將食譜中的數據複製到我的問題的底部。 –

+0

問題解決了,請參閱下面的答案。感謝你付出的努力。 –

回答

1

問題解決了。我想我應該使用coefplot包中預定義的multiplot函數,但實際上我必須使用已在R cookbook page上定義的自定義函數。

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { 
    require(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)) 
    } 
    } 
} 
+1

我正要說,如果我使用R Cookbook中定義的'multiplot',它對我很有用。一切順利。 – SlowLearner

6

通過使用名爲grid.arrange()的內置函數,可以更容易地做到這一點。你需要安裝一個名爲gridExtra使用它的包:

grid.arrange(p1,p2,p3,p4, ncol = 2, main = "Main title") 

Reference