在ggplot2中为每个构面设置不同的轴限制,而不使用scales =“ free”

skl*_*ene 2 r ggplot2

我想要的一般解决方案是能够为每个构面单独指定任意轴限制。

通过将比例尺设置为免费可获得基本功能。例如:

ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + facet_wrap(~clarity, nrow = 4, ncol = 2, scales = "free")

这实际上是一个非常不错的功能,但实际上并不总是那么有用。通常,我们想要的是在同一轴上具有可比较的变量子组。作为玩具示例,请考虑上面的钻石盒。我们可能希望第一列中的所有构面都具有相同的轴限制,而第二列中的所有构面都具有相同的轴限制(但与第一列不同)。

是否有使用标准 ggplot使用来完成此任务的解决方案。

Mar*_*son 5

在结束之前,我认为扩展@Axeman的建议很重要:facet_wrap 直接使用这可能无法实现,但是可以通过对所需的组进行分块并将其与缝合来实现此行为cowplot。在这里,我分为“低”质量和“高”质量,但是分组是任意的,可以随心所欲。可能希望稍微弄乱样式,但是cowplot为此可以使用默认的from :

library(cowplot)

lowQ <- 
  ggplot(diamonds %>%
           filter(as.numeric(clarity) <= 4)
         , aes(x = carat
               , y = price)) +
  geom_point() +
  facet_wrap(~clarity
             , nrow = 1)  


hiQ <- 
  ggplot(diamonds %>%
           filter(as.numeric(clarity) > 4)
         , aes(x = carat
               , y = price)) +
  geom_point() +
  facet_wrap(~clarity
             , nrow = 1)

plot_grid(lowQ, hiQ, nrow = 2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明