scales='free_y' has no effect in facet_grid?

4,259 views
Skip to first unread message

Carl Boettiger

unread,
Jun 4, 2014, 7:43:24 PM6/4/14
to ggp...@googlegroups.com
Hi folks,

Maybe I'm doing something stupid here; perhaps someone can set me straight.  I try (from the facet_grid documentation): 

    mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
    mt + facet_grid(. ~ cyl, scales = "free_y")

and I get something like:

which is the same as without any argument to scales.  I expected to get a uniquely scaled y axis to each plot.  (I believe I used to get that in an earlier version of ggplot2, but after upgrading today I cannot install said earlier version successfully now).  I thought this might be a ggplot2 bug so I filed this as an issue, https://github.com/hadley/ggplot2/issues/971, but perhaps it is just my misunderstanding so I thought to check here too (sorry for the redundant post).  

Any ideas?

Thanks much!

- Carl

Ito, Kaori (Groton)

unread,
Jun 4, 2014, 9:59:01 PM6/4/14
to Carl Boettiger, ggp...@googlegroups.com

Try:

mt + facet_wrap(~cyl ,scale="free_y")

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carl Boettiger

unread,
Jun 4, 2014, 10:18:44 PM6/4/14
to ggp...@googlegroups.com

(Missed the reply-all, sorry)

I am aware that this works with facet_wrap.  My question is about facet_grid.  

Sorry that in my attempt to provide a minimal reproducible example I have used one with a single factor so that facet_wrap does much the same thing, but I think you get the idea.

In earlier ggplot, scale="free_y" was a valid argument for facet_grid.  Now it seems to have no effect? 

---
Carl Boettiger
http://carlboettiger.info

sent from mobile device; my apologies for any terseness or typos

> On Jun 4, 2014 6:58 PM, "Ito, Kaori (Groton)" <Kaor...@pfizer.com> wrote:
>>
>> Try:
>>
>> mt + facet_wrap(~cyl ,scale="free_y")
>>
>>  
>>
>>  
>>
>> From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Carl Boettiger
>> Sent: Wednesday, June 04, 2014 7:43 PM
>> To: ggp...@googlegroups.com
>> Subject: scales='free_y' has no effect in facet_grid?
>>
>>  
>>
>> Hi folks,
>>
>>  
>>
>> Maybe I'm doing something stupid here; perhaps someone can set me straight.  I try (from the facet_grid documentation): 
>>
>>  
>>
>>     mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
>>
>>     mt + facet_grid(. ~ cyl, scales = "free_y")
>>
>>  
>>
>> and I get something like:
>>
>>  
>>

Ito, Kaori (Groton)

unread,
Jun 4, 2014, 10:20:17 PM6/4/14
to Carl Boettiger, ggp...@googlegroups.com

In facet_grid, your are displaying multi panels by column, which is sharing the y-axis. Therefore, “free-y” does not work (I think it is expected behavior, not a bug).

If you display panels by row, y-axis is not shared, therefore, free_y works. (but I think this is not what you wanted)

mt + facet_grid(cyl~. ,scale="free_y")

Carl Boettiger

unread,
Jun 4, 2014, 10:56:12 PM6/4/14
to Ito, Kaori (Groton), ggp...@googlegroups.com
Thanks, that's a good example that proves this is not a bug, y-axis can differ between rows but be the same within rows under that. Nevertheless, I think this change is unfortunate, since it seems like the entire grammar of facet_grid is then unavailable if your data are not so cooperative.  It would have seemed better to me if the new option were called something like free_y_rows, to indicate that the rows could have their own y's but be consistent across columns, rather than to change the old behavior.  

With apologies for not having a more minimal example, consider this data:

dat <- read.csv("example.csv")

ggplot(dat, aes(penalty_fraction, value, fill=penalty_fn, col=penalty_fn))  +
  stat_summary(fun.y = mean, geom = "line") + 
  facet_grid(statistic ~ timeseries, scale="free_y")


which creates a graph like this:




which could clearly benefit from some new scales.  I don't see how to construct that without creating each panel separately under this new behavior, which really seems to lose a lot of elegance from the grammar, particularly with large numbers of facets.  While I realize backwards compatibility is not guaranteed, It's also a bit of a shame to realize that figures from some old papers and examples of mine will no longer render correctly.  

Any suggestions as to how to work around this most elegantly?  

Thanks again for the explanations and the insight so far as well.

Carl

--
Carl Boettiger
UC Santa Cruz
http://carlboettiger.info/

Dennis Murphy

unread,
Jun 5, 2014, 4:18:09 AM6/5/14
to Carl Boettiger, Ito, Kaori (Groton), ggp...@googlegroups.com
Hi Carl:

There is a fundamental difference between facet_wrap() and facet_grid(): the former facets in one direction and 'wraps' the display in a ribbon-like fashion into multiple rows and columns when the number of levels is sufficiently high, whereas the latter facets by level combinations of two factors. The constraints on facet_grid() are more severe than those on facet_wrap() with respect to scaling. 

Your most recent example (BTW, you should have loaded library(httr) before your writeLines() statement since content() is a function from that package) showed that facet_grid() did exactly what it was meant to do: for each level of statistic, you got a different y-scale. Evidently, you want separate y-scales for each panel, but facet_grid() is not equipped to do that, as Kaori noted. If you use scales = "free" in facet_grid(), you'll get the following:

    * separate x-scales in each column of the display;
    * separate y-scales in each row of the display.

It has been this way for some time; I consulted the section of the 0.9.0 transition guide pertaining to facet_grid() and this is the way the scales behaved when scales = "free" was set. When scales are freed in one direction, the above behavior obtains for the free direction and the scaling in the other is fixed.
This is sensible behavior for panels arranged in a grid according to level combinations of two factors. 

You can achieve something like what I interpret you want by using facet_wrap() as follows:

ggplot(dat, aes(penalty_fraction, value, fill=penalty_fn, col=penalty_fn))  +
  stat_summary(fun.y = mean, geom = "line") +
  facet_wrap(~ statistic + timeseries, ncol = 2, scales = "free_y")

Unfortunately, the strip panel labeling functions are only available for facet_grid(), so if you don't like the strip labels you'll have to roll your own solution - the simplest is to create an interaction variable in the data and set the factor levels to match the labels you want...but there are other options.

HTH,
Dennis

Carl Boettiger

unread,
Jun 5, 2014, 10:04:30 AM6/5/14
to Dennis Murphy, Ito, Kaori (Groton), ggp...@googlegroups.com

Thanks!

---
Carl Boettiger
http://carlboettiger.info

sent from mobile device; my apologies for any terseness or typos

Reply all
Reply to author
Forward
0 new messages