如何消除ggplot facet上的死空间?

Jas*_*ter 1 r ggplot2

如何消除下方面板上的死角?我希望在面板"A"上消除10-12个月的死区,并 从面板"B"中消除1-9个死区.

我不想使用手动方法,因为我有很多经常更改的数据.我还需要面板相互适当缩放.面板"A"应该是面板"B"长度的三倍,因为它的x轴包含九个值,而面板"B"的x轴仅包含三个值.

理想情况下,面板"B"应该看起来像是"A"面板停止的延续.

library(tidyverse)
library(ggQC)

# Create the data frame
set.seed(5555)
Golden_Egg_df <- data.frame(month = 1:12,
                            egg_diameter = rnorm(n = 12, mean = 1.5, sd = 0.2), 
                            group = c(rep("A", 9), rep("B", 3)))

#>  month egg_diameter group
#>      1     1.346167     A
#>      2     1.410460     A
#>      3     1.219141     A
#>      4     1.578410     A
#>      5     1.648865     A
#>      6     1.207796     A
#>      7     1.490460     A
#>      8     1.330943     A
#>      9     1.431585     A
#>     10     1.433876     B
#>     11     1.673110     B
#>     12     1.632368     B

# Plot data frame on a control chart
XmR_Plot <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
  geom_point() + geom_line() + 
  stat_QC(method = "XmR") + 
  facet_grid(~group)
XmR_Plot
Run Code Online (Sandbox Code Playgroud)

这是情节,我不确定为什么它没有显示内联

mar*_*kus 5

我们可以使用参数space = "free"facet_grid

XmR_Plot <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
  geom_point() + geom_line() + 
  stat_QC(method = "XmR") + 
  facet_grid(~group, scales = "free", space = "free") +
  scale_x_continuous(breaks = 1:12,
                     labels = month.abb # delete this (and the comma in the line above) if you want numbers
                     )
XmR_Plot
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述