clauswilke / ggjoy

Deprecated. Please use ggridges.
GNU General Public License v2.0
293 stars 24 forks source link

respect facet_*(..., scales = "free_x") #7

Closed tjmahr closed 7 years ago

tjmahr commented 7 years ago

The facet_ functions allow the x and y limits to vary within each panel by using the scales argument.

ggjoy used to respect the per-panel free scales, but in the most recent update, it lost this ability.

library(dplyr)
iris_long <- iris %>%
  tidyr::gather(variable, value, -Species)

library(ggjoy)
ggplot(iris_long) +
  aes(x = value, y = Species) +
  geom_joy(rel_min_height = 0.01) +
  facet_wrap("variable")
#> Picking joint bandwidth of 0.575

unnamed-chunk-2-1

ggplot(iris_long) +
  aes(x = value, y = Species) +
  geom_joy(rel_min_height = 0.01) +
  facet_wrap("variable", scales = "free_x")
#> Picking joint bandwidth of 0.575

unnamed-chunk-2-2

# A plot where this scaling is respected
with_density <- iris_long %>%
  group_by(Species, variable) %>%
  do(ggplot2:::compute_density(.$value, NULL)) %>%
  ungroup() %>%
  rename(value = x)

ggplot(with_density) +
  aes(x = value, y = Species, height = scaled, group = Species) +
  geom_ridgeline(scale = 1.8) +
  facet_wrap("variable", scales = "free_x")

unnamed-chunk-2-3

devtools::session_info(pkgs = "ggjoy")
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.0 (2017-04-21)
#>  system   x86_64, mingw32             
#>  ui       RStudio (1.0.143)           
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       America/Chicago             
#>  date     2017-07-14
#> Packages -----------------------------------------------------------------
#>  package      * version date       source                           
#>  colorspace     1.3-2   2016-12-14 CRAN (R 3.4.0)                   
#>  dichromat      2.0-0   2013-01-24 CRAN (R 3.4.0)                   
#>  digest         0.6.12  2017-01-27 CRAN (R 3.4.0)                   
#>  ggjoy        * 0.1     2017-07-14 Github (clauswilke/ggjoy@3ba0469)
#>  ggplot2      * 2.2.1   2016-12-30 CRAN (R 3.4.0)                   
#>  graphics     * 3.4.0   2017-04-21 local                            
#>  grDevices    * 3.4.0   2017-04-21 local                            
#>  grid           3.4.0   2017-04-21 local                            
#>  gtable         0.2.0   2016-02-26 CRAN (R 3.4.0)                   
#>  labeling       0.3     2014-08-23 CRAN (R 3.4.0)                   
#>  lazyeval       0.2.0   2016-06-12 CRAN (R 3.4.0)                   
#>  magrittr       1.5     2014-11-22 CRAN (R 3.4.0)                   
#>  MASS           7.3-47  2017-02-26 CRAN (R 3.4.0)                   
#>  methods      * 3.4.0   2017-04-21 local                            
#>  munsell        0.4.3   2016-02-13 CRAN (R 3.4.0)                   
#>  plyr           1.8.4   2016-06-08 CRAN (R 3.4.0)                   
#>  RColorBrewer   1.1-2   2014-12-07 CRAN (R 3.4.0)                   
#>  Rcpp           0.12.11 2017-05-22 CRAN (R 3.4.0)                   
#>  reshape2       1.4.2   2016-10-22 CRAN (R 3.4.0)                   
#>  rlang          0.1.1   2017-05-18 CRAN (R 3.4.0)                   
#>  scales         0.4.1   2016-11-09 CRAN (R 3.4.0)                   
#>  stats        * 3.4.0   2017-04-21 local                            
#>  stringi        1.1.5   2017-04-07 CRAN (R 3.4.0)                   
#>  stringr        1.2.0   2017-02-18 CRAN (R 3.4.0)                   
#>  tibble       * 1.3.3   2017-05-28 CRAN (R 3.4.0)                   
#>  tools          3.4.0   2017-04-21 local                            
#>  utils        * 3.4.0   2017-04-21 local
clauswilke commented 7 years ago

Yes, stat_joy makes a joint density estimate, which may or may not be desired. You can recover the old behavior by using stat_density:

ggplot(iris_long) +
   aes(x = value, y = Species, height = ..density..) +
   geom_joy(rel_min_height = 0.01, stat="density") +
   facet_wrap("variable", scales = "free_x")
screen shot 2017-07-14 at 5 37 57 pm

Note that this graph looks different than yours because in mine the area under each curve is the same within each panel. (It's not necessarily the same across panels.)

This probably needs to be added to the documentation somewhere.

clauswilke commented 7 years ago

I've added a note about this issue to the documentation of geom_joy().

clauswilke commented 7 years ago

In the latest version on github master, this should now work with the default stat_joy.

library(dplyr)
iris_long <- iris %>%
  tidyr::gather(variable, value, -Species)

library(ggjoy)
ggplot(iris_long) +
  aes(x = value, y = Species) +
  geom_joy(rel_min_height = 0.01) +
  facet_wrap("variable", scales = "free_x")
screen shot 2017-07-23 at 11 39 34 pm

Density estimates are still made jointly within each panel, but separately among panels.