2015-09-02 43 views
1

我試圖製作一個由多個圖組成的圖。這些圖是用循環內的ggplot(庫(「ggplot2」))製作的,並保存在兩個列表中。第一個列表的圖應該在多槽的左側,第二個列表的右側有圖。 一般來說,我得到它爲其他六個分別作出的陰謀(plot1,...,plot6)工作。Multiplot有時不起作用 - 接收錯誤:美學必須是長度爲1或與數據長度相同長度

multiplot(p1,p2,p3,p4,p5,p6,cols=2) 

工作以及

plotlista <- list(p1,p2,p3) 
plotlistb <- list(p4,p5,p6)  
multiplot(c(plotlista,plotlistb),cols=2) 

但是,當我想的multiplot在循環中取得的地塊中,事後的multiplot不起作用。請參閱下面的一些代碼...發生

plotlist1 <- list() 
plotlist2 <- list() 
for(a in types){ 

... some data handling ... 

p5 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = names(aaa))) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = names(aaa), size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + ylim(-150, 400) + xlim(-150, 1000) + 
    scale_fill_manual(values=aaa) + 
    ggtitle(paste("type ",a,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

plotlist1[[which(types==a)]] <- p5 

... some data handling ... 

p55 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = names(aaa))) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = names(aaa), size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + ylim(-150, 400) + xlim(-150, 1000) + 
    scale_fill_manual(values=aaa) + 
    ggtitle(paste("type ",a,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

plotlist2[[which(types==a)]] <- p55 

filepath <- paste("byxxxx",Agglevel,a,".jpg",sep="") 
jpeg(filename = filepath , width = 750, height = 750) 
multiplot(plotlist=list(p5,p55),cols=2) 
dev.off() 

} 

multiplot(plotlist=c(plotlist1,plotlist2),cols=2) 

以下錯誤:

Error: Aesthetics must either be length one, or the same length as the dataProblems:names(aaa)

有13個貫穿循環,但即使我削減運行兩個發生同樣的錯誤。

循環中的以下幾行對於每次運行也都很好,並且正確保存了jpeg。

filepath <- paste("byxxxx",Agglevel,a,".jpg",sep="") 
jpeg(filename = filepath , width = 750, height = 750) 
multiplot(plotlist=list(p5,p55),cols=2) 
dev.off() 

它也有可能的multiplot情節p5p55以及listplot1[[13]]listplot2[[13]]這是在上次運行通過循環與製作:

multiplot(plotlist=list(p5,p55),cols=2) 
multiplot(plotlist=c(plotlist1[13],plotlist2[13]),cols=2) 

的的multiplot功能來自http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/

# 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)) 
    } 
    } 
} 

是否存在保存問題循環內的ggplot對象?

添加一個重複的例子:

## example 
library("ggplot2") 
library(grid) 


# 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) { 


    # 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)) 
    } 
    } 
} 
example1 <- read.table(text="x height w wm color colorcode 
A 74.87091 0 0.8477582 PT E6F598FF 
         B 75.7462 0.8477582 2.7575227 ES 51AAAEFF 
         C 154.96351 2.7575227 5.0641208 IT 9DD7A4FF 
         D 218.2934 5.0641208 5.582455 EL 3C93B8FF",header=TRUE) 
example1$colorcode <- paste("#",example1$colorcode,sep="") 
#example2 <- example1[-1,] 
example3 <- read.table(text=" x height w wm color colorcode 
         E 23.31359 0 2.619406 BG B41947FF 
         F 28.60724 2.619406 3.282477 HU FEE08BFF 
         G 33.30486 3.282477 3.292582 CY DB474CFF 
         H 34.40072 3.292582 24.22946 PL FDCC7AFF 
         I 42.86401 24.22946 26.141007 RO 9E0142FF 
         C 45.83487 26.141007 47.13058 IT 9DD7A4FF 
         J 46.48877 47.13058 48.152381 SI FDB869FF 
         K 47.74536 48.152381 51.518293 CZ FEEC9FFF 
         L 50.12508 51.518293 278.176833 FR 66C2A5FF 
         M 72.17257 278.176833 284.513883 DK 4C65ACFF 
         N 73.16484 284.513883 285.837194 SK FBA15BFF 
         O 75.94599 285.837194 314.661756 AT CEEB9CFF 
         P 77.39849 314.661756 339.818609 BL 5E4FA2FF 
         D 77.87686 339.818609 340.366524 EL 3C93B8FF 
         Q 78.60072 340.366524 623.890373 DE 3A7CB7FF 
         R 80.39367 623.890373 624.871331 EE F7874FFF 
         S 83.13369 624.871331 626.13241 LV E75A47FF 
         T 87.46645 626.13241 628.725496 LT F46D43FF 
         U 89.35913 628.725496 629.62701 FI FAFDB7FF 
         V 94.46328 629.62701 766.334365 NL B6E1A1FF 
         W 106.02335 766.334365 871.746899 IR 81CCA4FF 
         X 107.62544 871.746899 881.874454 SE F0F9A7FF 
         B 112.60728 881.874454 892.735907 ES 51AAAEFF 
         Y 127.36419 892.735907 991.546621 UK FEF8B4FF 
         Z 342.85699 991.546621 997.847212 PT E6F598FF",header=TRUE) 
example3$colorcode <- paste("#",example3$colorcode,sep="") 
#example4 <- example3[-1,] 
example1[,c("wm","w","height")] <- example1[,c("wm","w","height")] + 10 
#example2[,c("wm","w","height")] <- example2[,c("wm","w","height")] + 10 
example3[,c("wm","w","height")] <- example3[,c("wm","w","height")] + 10 
#example4[,c("wm","w","height")] <- example4[,c("wm","w","height")] + 10 

example.data <- list(example3,example1) 

listplota <- list() 
listplotb <- list() 

AxisX_max <- 1100 
AxisY_max <- 360 
AxisX_min <- 0 
AxisY_min <- 0 

for(i in 1:2){ 
    df.1 <- example.data[[i]] 

    p5 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = x)) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + 
    #ylim(-150, 400) + xlim(-150, 1000) + 
    ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + 
    scale_fill_manual(values=df.1$colorcode) + 
    ggtitle(paste("type ",i,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

listplota[[i]] <- p5 

    p55 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = x)) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + 
    #ylim(-150, 400) + xlim(-150, 1000) + 
    ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + 
    scale_fill_manual(values=df.1$colorcode) + 
    ggtitle(paste("type ",i,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

listplotb[[i]] <- p55 

} 

multiplot(plotlist=c(listplota,listplotb),cols=2) 

Error: Insufficient values in manual scale. 25 needed but only 4 provided.

+0

硬盤沒有看到什麼數據看起來像調試,但我的猜測(和錯誤的輸出),它在填充組件中出錯。可能最好在數據框中包含填充因子。 – Heroka

+0

感謝提示@Heroka。例如我改變了'p5 < - ggplot(df.1,aes(xmin = wm,xmax = w,ymin = 0,ymax = height,fill = x))... geom_text(aes(x =(wm + w )/ 2,y = 150,label = x,size = 16,angle = 90))+ ... theme(axis.ticks = element_blank(),legend.position =「none」)+ ylim(-150 ,400)+ xlim(-150,1000)+ scale_fill_manual(values = aaa)+ ggtitle(paste(「type」,a,sep =「 - 」))+ theme(plot.title = element_text(lineheight =。 8,face =「bold」))'aes fill和geom_text標籤。現在出現以下錯誤:手動縮放值不足。需要22個,但只提供了4個。 – sebastiann

+0

感謝您的努力。請記住,一個可重複的例子應該可以在任何人的機器上運行。如果我要調試您的代碼,我希望它會產生您報告的錯誤。目前情況並非如此。 – Heroka

回答

0

現在是工作...謝謝@Heroka

## example 
library("ggplot2") 
library(grid) 


# 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) { 


    # 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)) 
    } 
    } 
} 
example1 <- read.table(text="x height w wm color colorcode 
A 74.87091 0 0.8477582 PT E6F598FF 
         B 75.7462 0.8477582 2.7575227 ES 51AAAEFF 
         C 154.96351 2.7575227 5.0641208 IT 9DD7A4FF 
         D 218.2934 5.0641208 5.582455 EL 3C93B8FF",header=TRUE) 
example1$colorcode <- paste("#",example1$colorcode,sep="") 
#example2 <- example1[-1,] 
example3 <- read.table(text=" x height w wm color colorcode 
         E 23.31359 0 2.619406 BG B41947FF 
         F 28.60724 2.619406 3.282477 HU FEE08BFF 
         G 33.30486 3.282477 3.292582 CY DB474CFF 
         H 34.40072 3.292582 24.22946 PL FDCC7AFF 
         I 42.86401 24.22946 26.141007 RO 9E0142FF 
         C 45.83487 26.141007 47.13058 IT 9DD7A4FF 
         J 46.48877 47.13058 48.152381 SI FDB869FF 
         K 47.74536 48.152381 51.518293 CZ FEEC9FFF 
         L 50.12508 51.518293 278.176833 FR 66C2A5FF 
         M 72.17257 278.176833 284.513883 DK 4C65ACFF 
         N 73.16484 284.513883 285.837194 SK FBA15BFF 
         O 75.94599 285.837194 314.661756 AT CEEB9CFF 
         P 77.39849 314.661756 339.818609 BL 5E4FA2FF 
         D 77.87686 339.818609 340.366524 EL 3C93B8FF 
         Q 78.60072 340.366524 623.890373 DE 3A7CB7FF 
         R 80.39367 623.890373 624.871331 EE F7874FFF 
         S 83.13369 624.871331 626.13241 LV E75A47FF 
         T 87.46645 626.13241 628.725496 LT F46D43FF 
         U 89.35913 628.725496 629.62701 FI FAFDB7FF 
         V 94.46328 629.62701 766.334365 NL B6E1A1FF 
         W 106.02335 766.334365 871.746899 IR 81CCA4FF 
         X 107.62544 871.746899 881.874454 SE F0F9A7FF 
         B 112.60728 881.874454 892.735907 ES 51AAAEFF 
         Y 127.36419 892.735907 991.546621 UK FEF8B4FF 
         Z 342.85699 991.546621 997.847212 PT E6F598FF",header=TRUE) 
example3$colorcode <- paste("#",example3$colorcode,sep="") 
#example4 <- example3[-1,] 
example1[,c("wm","w","height")] <- example1[,c("wm","w","height")] + 10 
#example2[,c("wm","w","height")] <- example2[,c("wm","w","height")] + 10 
example3[,c("wm","w","height")] <- example3[,c("wm","w","height")] + 10 
#example4[,c("wm","w","height")] <- example4[,c("wm","w","height")] + 10 

example.data <- list(example3,example1) 

listplota <- list() 
listplotb <- list() 

AxisX_max <- 1100 
AxisY_max <- 360 
AxisX_min <- 0 
AxisY_min <- 0 

# color <- data.frame(example3$colorcode) 
# color <- color$colorcode 
# rownames(color) <- example3$x 

for(i in 1:2){ 
    df.1 <- example.data[[i]] 

    p5 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = colorcode)) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + 
    #ylim(-150, 400) + xlim(-150, 1000) + 
    ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + 
    #scale_fill_manual(values=df.1$colorcode) + 
    scale_fill_identity() + 
    ggtitle(paste("type ",i,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

listplota[[i]] <- p5 

    p55 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = colorcode)) + 
    geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + 
    theme_bw() + labs(x = "cummulated", y = "costs") + 
    theme(axis.ticks = element_blank(), legend.position = "none") + 
    #ylim(-150, 400) + xlim(-150, 1000) + 
    ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + 
    #scale_fill_manual(values=df.1$colorcode) + 
    scale_fill_identity() + 
    ggtitle(paste("type ",i,sep="- ")) + theme(plot.title = element_text(lineheight=.8, face="bold")) 

listplotb[[i]] <- p55 

} 

multiplot(plotlist=c(listplota,listplotb),cols=2) 
相關問題