2

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")

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).

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

skleene
  • 389
  • 3
  • 13
  • 2
    I'm not aware of any (absent lots of manual grid code tinkering) but here is some [related](http://stackoverflow.com/q/18046051/324364) [reading](https://github.com/hadley/ggplot2/issues/187). – joran Aug 30 '16 at 14:19
  • 1
    No standard solution. I would probably produce both columns separately and use `cowplot` to stick them together. – Axeman Aug 30 '16 at 14:42
  • Seems to me that this would be programmed in the same manner as `labeller`. The request to have a by-column action is similar to the `.rows` and `.cols` margin specification. Voting to close as really a feature request and SO is not the correct place for those. – IRTFM Aug 30 '16 at 15:36
  • I'm voting to close this question as off-topic because it's really a feature request that has been made before (as researched by @joran), accepted as valid, but awaiting implementation. – IRTFM Aug 30 '16 at 15:39
  • I suggest this question not be closed. The answer to the question is that a solution does not exist in *standard* ggplot but there are some out of the box solutions. Also, the issue is much more transparently addressed here than [here](http://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2) – skleene Sep 02 '16 at 15:19

1 Answers1

5

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)

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48