首页 > 解决方案 > 如何消除ggplot方面的死区?

问题描述

如何消除下方刻面面板上的死区?我希望在面板“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

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

标签: rggplot2

解决方案


我们可以使用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

在此处输入图像描述


推荐阅读