首页 > 解决方案 > 如何根据使用两个因素的因素级别在 facet_wrap 中打破新行?

问题描述

如何使每个新行都以新的因子水平开始?目前它基于批次和样本进行包装,但不会在批次的新因子水平上中断。尝试 'facet_grid(~batch~sample)' 时,有许多不需要的空面板。

R 的结果和 Photoshop 的所需结果。

在此处输入图像描述

编辑以包含较小的可重现数据集

df <- data.frame("sample" = c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11"), 
                 "length_um" = c(1.8,1.9,0.52,0.75,0.14,0.95,0.84,0.46,0.25,0.13,0.31),
                 "breadth_um" = c(1.44,1.52,0.41,0.60,0.12,0.76,0.68,0.37,0.20,0.11,0.25),
                 "batch" = c("batch01","batch01","batch01","batch01","batch02","batch02","batch02","batch02","batch02","batch03","batch03"))

df <- df %>%
  mutate_if(sapply(df, is.character), as.factor)

ggplot(df, aes(x=length_um,y=breadth_um)) +
  geom_point()+
  facet_wrap(~batch~sample) 

标签: rggplot2facet

解决方案


尝试替换facet_wrap(~batch~sample)facet_grid(~batch~sample).


推荐阅读