簡體   English   中英

如何使用plot_grid放置沒有任何空間的繪圖?

[英]How to put plots without any space using plot_grid?

我正在安排2x2圖。 這些圖共享相同的軸,所以我想將它們放在一起,例如

這段代碼:

library(ggplot2)
library(cowplot)

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


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

plot_grid(plot, plot, plot, plot, align = "hv", ncol = 2)

產生

在此輸入圖像描述

但我喜歡這樣的東西:

在此輸入圖像描述

我怎樣才能獲得類似的結果?

我認為這是來自egg包的ggarrange()函數的情況。 使用plot_grid()執行此plot_grid()需要無休止的擺弄,並且不值得。

(技術原因是plot_grid()保持網格中每個繪圖的總面積不變,但如果某些繪圖有x軸而其他繪圖沒有,則它們會占用不同的區域。可以嘗試通過使用rel_heights參數但是沒有好的方法來計算rel_heights的正確值,所以它將是反復試驗。相比之下, ggarrange()分別查看繪圖面板和周圍元素,並確保繪圖面板具有相同的大小。 )

這是使用ggarrange()的代碼:

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


pbase <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_bw()

ptopleft <- pbase +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 0, 0, 5.5),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

ptopright <- pbase +
  scale_y_continuous(position = "right") +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 5.5, 0, 0),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

pbottomleft <- pbase +
  theme(plot.margin = margin(0, 0, 5.5, 5.5))

pbottomright <- pbase +
  scale_y_continuous(position = "right") +
  theme(plot.margin = margin(0, 5.5, 5.5, 0))

library(egg)      
ggarrange(ptopleft, ptopright,
          pbottomleft, pbottomright,
          ncol = 2)

在此輸入圖像描述

兩條評論:

  1. 要刪除頂部圖上繪圖面板下方的最后一點空間,我們需要將x軸移動到頂部,即使我們沒有顯示它。 這是主題機制的一個奇怪的限制。 我們不能完全擺脫一個軸。

  2. 我不是共享軸標題的忠實粉絲,就像你的例子一樣。 我認為每個軸都應該有一個標題。 如果您想要共享軸標題,為什么不使用分面機制?

您可以plot.margin每個圖設置微妙的plot.margin ,然后grid.arrange並添加實驗。

library(ggplot2)
library(grid)
library(gridExtra)

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_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.text.x = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(5.5, 5.8, -50, 5.5), "pt")) 

plot2 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.text.x = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(5.5, 5.5, -50, 5.5), "pt")) +
  scale_y_continuous(position = "right")

plot3 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(-50, 5.8, -50, 5.5), "pt")) 

plot4 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(-50, 5.5, -50, 5.5), "pt")) +
  scale_y_continuous(position = "right")

grid.arrange(grobs = list(plot1, plot2, plot3, plot4), ncol = 2, bottom = 'Index', left = 'Value', right = 'Value')

最后的情節 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM