Problem with facet_grid and scales = 'free'

77 views
Skip to first unread message

Bill Harris

unread,
Sep 30, 2016, 12:35:04 PM9/30/16
to ggplot2
I'm trying to graph data in facets, and that data has widely varying scales, seemingly calling for the use of "scales = 'free'".  Here's a simple example.

Enter code here.library(ggplot2)
library
(scales)
library
(dplyr)
library
(broom)
data
(mtcars)

mtcars
%>%
    mutate
(gpm = 1/mpg) %>%
    ggplot
(aes(x = wt, y = gpm)) +
    geom_point
() +
    facet_grid
(am ~ cyl, scales = 'free')

## > sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1

## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252  
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base    

## other attached packages:
## [1] broom_0.4.1   dplyr_0.5.0   scales_0.4.0  ggplot2_2.1.0

## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.7      magrittr_1.5     mnormt_1.5-4     munsell_0.4.3  
##  [5] colorspace_1.2-6 lattice_0.20-34  R6_2.1.3         stringr_1.1.0  
##  [9] plyr_1.8.4       tools_3.3.1      parallel_3.3.1   grid_3.3.1      
## [13] gtable_0.2.0     nlme_3.1-128     psych_1.6.9      DBI_0.5-1      
## [17] digest_0.6.10    lazyeval_0.2.0   assertthat_0.1   tibble_1.2      
## [21] reshape2_1.4.1   tidyr_0.6.0      labeling_0.3     stringi_1.1.1  
## [25] compiler_3.3.1   foreign_0.8-67
   
..

On my screen, I see 2 rows of 3 graphs each.  The abscissas have different scales in each column, and the ordinates have different scales in each row.  However, each of the graphs in the two rows would seem to warrant different scales on their ordinate, but they share the same one.  Similarly, each of the graphs in the three columns would seem to warrant different scales on their abscissas, but they share a common one.

If I run this code, instead, I get the scaling I expect, but the facet headers are not as I want.

mtcars %>%
    mutate
(gpm = 1/mpg) %>%
    ggplot
(aes(x = wt, y = gpm)) +
    geom_point
() +
    facet_wrap
(am ~ cyl, scales = 'free')


Is it possible to do what I want?  The documentation for the scales argument on the facet_grid and facet_wrap pages is not identical, and facet_grid may be not capable of doing what I want.  Still, my real data produces one graph that goes from about 1 to 1,000 and another that goes from perhaps 20,000 to 60,000, and the corresponding facet_grid command above is unusable.  Having a command that handled free scales as facet_wrap does and graph headers as facet_grid does would be really nice.

Thanks,

Bill

Brian

unread,
Sep 30, 2016, 5:34:22 PM9/30/16
to Bill Harris, ggplot2
Hi Bill,

maybe I didn't understand, but it seems to be expected behavior. The scales = "free" argument is such that the y-axes of the two rows are independent of one another. The same applies for the y-axis.

You can do what you want in R. ggplot does what I want. Are you really sure you want to have all axes being "free" everywhere?

Brian

Bill Harris

unread,
Sep 30, 2016, 5:57:56 PM9/30/16
to ggplot2, bill_...@facilitatedsystems.com
Brian,

I switched my report to use facet_wrap, with the number of columns set to cause the graph to match the layout from facet_grid.  While I don't find it as easy to view as facet_grid (it's not as obvious that each row of graphs has a distinct property), it handles the scaling as I want, and I'm getting comfortable with it.  I really do see the advantage in having it both ways, and I was expecting to get the look of facet_grid and, optionally, the scaling of facet_wrap.  I can live with this, now that I understand.

Thanks,

Bill
Reply all
Reply to author
Forward
0 new messages