Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facet_grid() supporting a different scale per each facet. #2747

Closed
wants to merge 2 commits into from

Conversation

zeehio
Copy link
Contributor

@zeehio zeehio commented Jul 10, 2018

Motivation

The ability to use different scales in different facets has been a long time feature request (e.g. two years ago in #1613) and back in 2010 here.

The main reason to ask for this feature is to be able to compare different magnitudes, as would be desired for instance in this plot:

imatge

This kind of plots are usual in scientific environments (for instance for comparing concentrations, #1613 (comment)) and currently require a lot of fine tuning, using grid.arrange and playing with legends to make sure each subplot is aligned.

Example of use:

  library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
  library(scales)
  library(ggplot2)
# Simple tidy data frame, with 4 subjects. We measure three variables on each
# subject (namely: SomeValue, Percent and Scientific).
mydf <- dplyr::data_frame(Subject=rep(c("Alice", "Bob", "Charlie", "Diane"), each=3),
                          Magnitude=rep(c("SomeValue", "Percent", "Scientific"), times=4),
                          Value=c(c(170,0.6,2.7E-4),
                                  c(180, 0.8, 2.5E-4),
                                  c(160, 0.71, 3.2E-4),
                                  c(159, 0.62, 3E-4)),
                          Gender=rep(c("Female", "Male", "Male", "Female"), each=3))

# This is how the df looks like:
head(mydf)
#> # A tibble: 6 x 4
#>   Subject Magnitude      Value Gender
#>   <chr>   <chr>          <dbl> <chr> 
#> 1 Alice   SomeValue  170       Female
#> 2 Alice   Percent      0.6     Female
#> 3 Alice   Scientific   0.00027 Female
#> 4 Bob     SomeValue  180       Male  
#> 5 Bob     Percent      0.8     Male  
#> 6 Bob     Scientific   0.00025 Male
#  Subject  Magnitude   Value Gender
#1   Alice  SomeValue 1.7e+02 Female
#2   Alice    Percent 6.0e-01 Female
#3   Alice Scientific 2.7e-04 Female
#4     Bob  SomeValue 1.8e+02   Male
#5     Bob    Percent 8.0e-01   Male
#6     Bob Scientific 2.5e-04   Male

scales_y <- list(
  Percent = scale_y_continuous(labels=percent_format()),
  SomeValue = scale_y_continuous(),
  Scientific = scale_y_continuous(labels=scientific_format())
)

# Here the percent (top) is well written, the other ones don't make sense
ggplot(mydf) +
  geom_point(aes(x=Subject, y=Value, color=Gender)) +
  facet_grid(Magnitude~., scales = list(y = scales_y))

Created on 2018-07-10 by the reprex package (v0.2.0).

Merge vs not merge reasons:

  • Merge:
    • The diff is small.
    • Backwards compatibility is preserved.
  • Not merge:
    • This feature could fit in another package, providing a custom facet. There would be some code duplication, but it looks easy to do (I have a proof of concept).

This can be used in facetting plots that share one axis but have different units in the other axis (@zeehio, tidyverse#1613).
@clauswilke
Copy link
Member

I have a number of questions:

  1. Should this be implemented for facet_wrap() as well?

  2. What happens with coord_fixed()?

  3. What happens when people try to set individual titles for the different scales?

  4. When I have a grid of plots with multiple columns and rows, can I set a different scale for each plot?

  5. What happens when facets are renamed via a labeller function?

  6. What happens when people define a scale outside of the facet_grid() call, and does it matter whether it's called before or after facet_grid()?

This may be a case where implementing this first in a package to gain some experience with this feature and explore the best interface may be a good idea.

@zeehio
Copy link
Contributor Author

zeehio commented Jul 11, 2018

Thanks @clauswilke . Following your advice I have created https://github.com/zeehio/facetscales and I have opened an issue with your comments (zeehio/facetscales#1)

I will try to address those issues (and other issues to come) and get back to you with an improved proposal. For now, I will close the pull request and redirect people from #1613 to my package. Let's see what comes out from this. 👍

Thanks for your feedback!

@zeehio zeehio closed this Jul 11, 2018
@lock
Copy link

lock bot commented Jan 7, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants