我正在尝试通过
ggplot
中的facet_wrap
生成两个面板图,并以以下方式对齐它们(注意:面板A需要保持空白):
然而,我注意到面板B的y轴与面板C的最后两个图的y轴不完全对齐(尽管在两个面板中都使用了选项axis = 'lb'
)。
代码:
# Load libraries
library(tidyverse)
library(cowplot)
# Create two facetted plots
p1 <- ggplot(data = diamonds, aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol = 5) +
geom_point(size=0.5)
p2<- ggplot(data = filter(diamonds, price < 900 & (cut == "Fair" | cut == "Good" )), aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol = 2) +
geom_point(size=0.5)
# Create panel A and panel B
a <- plot_grid(NULL, p2, labels = c("A", "B"), axis = 'lb', ncol = 2, rel_widths = c(3,2))
# Create a combined panel of 'a' and panel C
plot_grid(a, p1, labels = c("", "C"), axis = 'lb', ncol = 1, rel_heights = c(1,1))
起初,我以为这与y轴标签有关,但是去掉标签并没有解决问题。
问题
是什么导致了这种行为,如何使用cowplot
包对使用facet_wrap
生成的图形进行对齐。
期望输出
我希望B面板的y轴垂直对齐于C面板中最后两个图形的y轴(即在红线处)。