Set common scale parameters outside of the scale function
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- r
- Domain
- data-visualization
Research direction
Start by reading R/limits.r, R/labels.r, R/plot-build.r, and R/layout.R, then reproduce the two examples in the issue to understand their differing scale behavior. The issue does not define a concrete API or implementation target, so completion would require agreeing on a generic approach and its expected behavior for limits, labels, expansions, breaks, and discrete scales.
Written by the indexing model from the issue text.
Description
A problem that pops up repeatedly in different contexts is that we may want to adjust certain scale parameters without explicitly calling a scale function. We already have some support for this, e.g. with xlim() to set limits on the x position scale or xlab() to set its title, but there are numerous other scenarios where the same problem arises. Here are some examples:
- We may want to set scale expansions: #3962
- We may want to set breaks or limits, including for non-position scale functions: https://github.com/tidyverse/ggplot2/pull/3973#issuecomment-621879755
- We may want to specify
drop = FALSEon discrete scales: https://github.com/tidyverse/ggplot2/issues/4268#issuecomment-729224543
It would be good if we could come up with a generic approach to these issues. I note that xlim() and xlab() use entirely different implementation approaches currently, so that's not promising:
-
xlim()andylim()use theggplot2:::limits()function to automatically generate a scale function and give it the appropriate parameter settings:
https://github.com/tidyverse/ggplot2/blob/3be0acce79dc295d5470d949c60de1b9c84a6fc9/R/limits.r#L112-L160 -
xlab()andylab()ultimately feed their info to theupdate_labels()function, which writes to a specialplot$labelsfield that the layout reads from:
https://github.com/tidyverse/ggplot2/blob/3aa29375167946b970692e42241e9aabaee3ae03/R/labels.r#L13-L17
https://github.com/tidyverse/ggplot2/blob/660aad2db2b3495ae0d8040915a40d247133ffc0/R/plot-build.r#L170
https://github.com/tidyverse/ggplot2/blob/b76fa9639215785f8e94874d3bdf02225bae898a/R/layout.R#L107-L113
Neither approach generalizes directly to the other use cases. I also have concerns about the way xlim() and ylim() are implemented, because the fact that they add explicit scales functions is not obvious and can lead to strange behavior. See e.g. the following reprex, where in the first plot the call to scale_x_continuous() overwrites the xlim() call but in the second plot the xlab() call is not overwritten:
library(ggplot2)
ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
xlim(100, 300) +
scale_x_continuous(name = "Displacement")
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.

ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
xlab("Displacement") +
scale_x_continuous(limits = c(100, 300))
#> Warning: Removed 16 rows containing missing values (geom_point).

Created on 2020-11-17 by the reprex package (v0.3.0)
- Dominant language
- R
- Stars
- 7k
- Forks
- 2.1k
- Avg merge
- 59m
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tidyverse/ggplot2
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
internals :mag_right:
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
themes :dancer:
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
documentation themes :dancer:
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
scales :snake:
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
All issues in tidyverse/ggplot2
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·