为 R 中的多面板分面 ggplot 设置相同的 y-lim

Set same y-lim for multi panel facet ggplot in R

我正在生成一个多面图,我想为所有面板设置相同的 y lim 轴 (0,250000),但保持 x 轴的格式与下面相同

这是我的代码:

ggplot(seqDepthDF_melt,aes(x=SampleID,y=value))+
  geom_bar(stat="identity",aes(fill=Step))+
  ylab("Million PE reads") +
  theme_bw()+
  facet_wrap(~ SampleName,scales = "free")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=rel(0.7)))+
  scale_fill_manual(values=wes_palette(n=3, name="GrandBudapest1"))

您有什么建议吗?

facet_wrap 的文档说:

scales: 尺度应该是固定的("fixed",默认值),自由的("free"),还是一维自由的("free_x","free_y")?

因此,当您指定 "free_x" 时,它在 x 维度上是自由的,但为 y-dimension 上的所有绘图提供相同的比例。

我们可以使用 mtcars 数据集来证明这一点:

library(ggplot2)

ggplot(mtcars, aes(mpg, gear)) +
  geom_point() +
  facet_wrap(~ am) +
  ggtitle("Fixed scales")

ggplot(mtcars, aes(mpg, gear)) +
  geom_point() +
  facet_wrap(~ am, scales = "free") +
  ggtitle("Free scales")

ggplot(mtcars, aes(mpg, gear)) +
  geom_point() +
  facet_wrap(~ am, scales = "free_x") +
  ggtitle("Free scales on x-axis")