首页 文章

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

提问于
浏览
2

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

通过将标度设置为空闲来获得基本功能 . 例如:

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

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

是否有使用标准ggplot用法实现此目的的解决方案 .

1 回答

  • 4

    在关闭之前,我认为扩展@Axeman的建议非常重要:直接使用_2569446可能无法做到这一点,但是可以通过分块你想要的组并用 cowplot 将它们拼接在一起来实现 . 在这里,我分为"low"和"high"质量,但分组是任意的,可以是你想要的任何东西 . 可能想要稍微混淆一下风格,但是 cowplot 的默认值是可以接受的:

    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)
    

    enter image description here

相关问题