我有一些數據被分為25個類別和6個父組,每個父組的類別數量不均勻(3到5)。對于每個父組,我使用facet_wrap根據類別創建單獨的面板,然后使用grid.arrange將父組圖顯示在一起。問題在于,由于每個父組中的類別數量不同,打印寬度會自動調整大小。是否有辦法手動設置打印寬度或創建空面板,以便寬度一致?
這里有簡單的娛樂活動:
library(dplyr)
library(gridExtra)
x <- c(1, 2, 3, 4, 1, 2)
y <- c(3, 3, 3, 3, 3, 3)
category <- c("category a", "category b", "category c", "category d", "category e", "category f")
group <- c("group one", "group one", "group one", "group one", "group two", "group two")
df <- data.frame(x, y, category, group)
group_one <- df %>% filter(group=="group one")
g1 <- ggplot(group_one, aes(x=x, y=y)) +
geom_point() +
facet_wrap(~category, nrow=1) +
labs(title="Group One")
group_two <- df %>% filter(group=="group two")
g2 <- ggplot(group_two, aes(x=x, y=y)) +
geom_point() +
facet_wrap(~category, nrow=1) +
labs(title="Group Two")
grid.arrange(g1, g2)
下面是上面代碼產生的結果-注意面板寬度不均勻。如何使兩組面板的寬度與第一組面板的寬度相同,且右側有空白?
如果有一個更簡單的方法來創建這種類型的情節安排與子目根據組,我很樂意聽到它!謝謝
一種選擇是利用
patchwork
包。在您的示例代碼中,您可以作為一種更具普遍性的方法,您可以
plot_layout
設置每個子繪圖和間隔的寬度,以便面板對齊。為了使示例更有趣,我添加了第三組: