为ggplot2中的每个构面设置不同的轴限制,而不使用scales ="free". [英] Setting different axis limits for each facet in ggplot2 not using scales = "free"

查看:121
本文介绍了为ggplot2中的每个构面设置不同的轴限制,而不使用scales ="free".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The general solution that I would like is to be able to specify arbitrary axis limits for each facet independently.

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

Basic functionality is obtained by setting the scales to be free. For example:

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

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

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

This is actually a really nice feature, but in practice is not always that useful. Often what we want is to have subgroups of variables comparable on the same axis. As a toy example, consider the diamonds case above. We might want to have all facets in column one have the same axis limits and all the facets in column two have the same axis limits (but different from column one).

是否有一种使用 standard ggplot来实现此目标的解决方案.

Is there a solution for accomplishing this with standard ggplot usage.

推荐答案

在此之前,我认为通过@Axeman扩展建议很重要:直接使用facet_wrap可能无法实现,但是行为可以通过对所需的组进行分块并将其与cowplot拼接在一起即可实现.在这里,我分为低"和高"质量,但是分组是任意的,并且可以是您想要的任何分组.可能想稍微弄乱样式,但是为此目的cowplot的默认设置是可以接受的:

Before this is closed, I think it is important to expand the suggestion by @Axeman : this may not be possible with facet_wrap directly, but the behavior can be achieved by chunking the groups you want and stitching them together with cowplot. Here, I separated into "low" and "high" quality, but the grouping is arbitrary and could be whatever you wanted. Likely want to mess with the style a bit, but the default from cowplot is acceptable for this purpose:

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)

这篇关于为ggplot2中的每个构面设置不同的轴限制,而不使用scales ="free".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆