2017-12-03 340 views
2

我有7個地塊,我把它們分成2列,一個有3個地塊,另一個有4個地塊。我使用cowplotplot_grid。結果幾乎是完美的,但是,有3個地塊的列有更大的地塊。如何縮放此列以在所有圖中獲得相同的大小並對齊每列的第一個和最後一個圖?如何將嵌套的plot_grid縮放到相同的大小?

library(ggplot2) 
library(cowplot) 

Value <- seq(0,1000, by = 1000/10) 
Index <- 0:10 
DF <- data.frame(Index, Value) 

plot1 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot2 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot3 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot4 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot5 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot6 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot7 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

col1 <- plot_grid(plot1, plot2, plot3, align = "v", ncol = 1) 
col2 <- plot_grid(plot4, plot5, plot6, plot7, align = "v", ncol = 1) 
plot_grid(col1, col2, ncol = 2, align = "hv") 

enter image description here

回答

4

你可以把一個空積成你的第一列:

col1 <- plot_grid(plot1, plot2, plot3, NULL, align = "v", ncol = 1) 

但是,對我來說,這不是一個嵌套的情節格柵的情形。嵌套的小區網格專門用於將小區與複雜的安排相結合,例如一列中的一個圖橫跨另一列中的兩行。你想要的東西,我會做一個plot_grid()通話,就像the other comment提示:

plot_grid(plot1, plot4, plot2, plot5, plot3, plot6, NULL, plot7, 
      ncol = 2, align = "hv") 

enter image description here

也請注意,當您指定一個特定的縱橫比align選項不起作用。您可以指定縱橫比或對齊。在大多數情況下,沒有必要指定寬高比,您可以在保存圖形時執行此操作。功能save_plot()特別需要寬高比選項(應用於整個圖像,但不是繪圖區域)。

+0

剛剛意識到你是cowplot的作者 - 這是一個很棒的包裝,謝謝! – Aaron

+0

難道不可能從具有更多圖的網格繼承佈局並將其應用於另一個?最後,轉移調整大小的地塊,使其居中。理論上聽起來很容易,但我也沒有得到它的工作。對'cowplot' :)來說會是一個很大的提升:) –

4

我剛剛在上週做了這個!嘗試對plot_grid進行一次調用,使用NULL來指定空白點。

myplotlist <- list(plot1, plot2, plot3, NULL, plot4, plot5, plot6, plot7, plot8) 
plot_grid(plotlist=myplotlist, align = "v", ncol = 2) 
相關問題