2017-07-01 38 views
0

我試着去這個圖我在ggplot做結合[![在這裏輸入的形象描述] [1] [1]混合ggplot在多頁圖表在PDF

多帶幾個曲線圖(如下圖所示的例子)在一個PDF文件中。但是第一張圖在一頁上,下一張圖在第2頁上,圖3在第3頁上等等。

[![在這裏輸入的形象描述] [2] [2]

我的問題是,我似乎無法爲我不斷收到一個錯誤,我不能打開PDF中的圖表1個PDF文件合併文件。 香港專業教育學院試圖修改代碼,香港專業教育學院在這裏看到: Printing multiple ggplots into a single pdf, multiple plots per page

,但它似乎無法工作

(這部分是4個圖形中1頁)

pdf("plots.pdf", onefile = TRUE) 

plot1 <- ggplot(data = FBMKLCI.df) + 
theme_minimal() + 
    geom_line(aes(x = Date, y = PX_LAST., color = 
PE)) + 
    scale_color_continuous(low = 'green', high='red') + 
    labs(y="", colour = "PE") + 
    theme(legend.position = 'bottom', 
     plot.title = element_text(colour = 'blue', face = 'bold'), 
     legend.key.width = unit(1, "cm")) + 
    ggtitle('FBMKLCI') 


plot2<- ggplot(data = FBM70.df) + 
    theme_minimal() + 
    geom_line(aes(x = Date, y = PX_LAST., color = 
PE)) + 
    scale_color_continuous(low = 'green', high='red') + 
    labs(y="",colour = "PE")+ 
    theme(legend.position = 'bottom', 
     plot.title = element_text(colour = 'blue', face = 'bold'), 
     legend.key.width = unit(1, "cm")) + 
    ggtitle('FBM70') 

plot3 <- ggplot(.... 

plot4<-... 

grid.arrange(plot1, plot2, plot3, plot4, ncol=2) 

(這部分是對於下面的圖表)

p <- list() 

for(i in 1:3) { 
    p[[i]] <- list() 

    p[[i]][[1]] <- ggplot(data = plot1) + 
    theme_minimal() + 
    facet_wrap(~Sector, nrow = 5, scales="free_y") + 
    geom_line(aes(x = Date, y = BEST_EPS.BEST_FPERIOD_OVERRIDE.1GY, color = 
    Sector)) + 
    theme(legend.position="none") 

    p[[i]][[2]] <- ggplot(data = plot2) + 
    theme_minimal() + 
    facet_wrap(~Sector, nrow = 5, scales="free_y") + 
    geom_line(aes(x = Date, y = eps.rev3mo, color = Sector)) + 
    theme(legend.position="none") 

    p[[i]][[3]] <- ggplot(data = plot3) + 
    theme_minimal() + 
    facet_wrap(~Sector, nrow = 5, scales="free_y") + 
    geom_line(aes(x = Date, y = eps.rev3mo, color = Sector)) + 
    theme(legend.position="none") 

} 

print(p) 

dev.off() 

我提前道歉,因爲這是我第一次使用ggplot2。非常感謝並提前感謝您的幫助。

+0

'marrangeGrob'可以在這裏幫助 – user20650

+0

謝謝,但我如何結合不同網頁的圖形?我已經設計了佈局,只需要它們在1個文件中。 –

回答

1

也許這會有所幫助,

enter image description here

library(ggplot2) 
library(gridExtra) 

page1 <- replicate(4, ggplot(), simplify = FALSE) 
other <- replicate(3, replicate(6, ggplot(), simplify = FALSE), simplify = FALSE) 

pdf("multipage.pdf", width=6, height = 4) 
grid.arrange(grobs = page1, ncol=2) 
print(marrangeGrob(grobs = unlist(other, recursive = FALSE), ncol=3,nrow=2)) 
dev.off()