-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Allow separate expansion values for lower and upper range limits (v2) #1805
Allow separate expansion values for lower and upper range limits (v2) #1805
Conversation
The `expand` argument for `scale_*_continuous()` and `scale_*_discrete()` now accepts separate expansion constants for the lower and upper range limits. This is useful for creating bar charts where the bottom of the bars are flush with the x axis but the bars still have some (automatically calculated amount of) space above them: ```R ggplot(mtcars) + geom_bar(aes(x = factor(cyl))) + scale_y_continuous(expand = c(0, 0, 0.1, 0)) ``` It can also be useful for line charts, e.g. for counts over time, where one wants to have a ’hard’ lower limit of y = 0, but leave the upper limit unspecified (and perhaps differing between panels), but with some extra space above the highest point on the line. (With symmetrical limits, the extra space above the highest point could cause the lower limit to be negative.) The syntax for the multiplicative and additive expansion constants has been changed from `c(m, a)` to `c(m_lower, a_lower, m_uppper, a_upper)`. The old syntax will still work, as length 2 vectors `c(m, a)` are expanded to `c(m, a, m, a)` and length 3 vectors are expanded from `c(m1, a1, m2)` to `c(m1, a2, m2, a1)`. (@huftis, tidyverse#1669)
…etric_expand_argument_v2
I think |
@hadley The reason I used |
Yes, a helper function (possibly returning an S3 object or just a vector). I don't know what to call it either 😞 |
Maybe |
Are you still interested in this PR? I'm going to be working on ggplot2 off and on for a bit. |
@hadley Yes, I’m still interested; I’ve just been too busy to take a look at it (or, really, it’s been too low on my priority list for too long). I hope to have a new go at it sometime in the next few weeks. When are you planning a test/beta release of the next |
vectors. Instead of having to manually specify an `expand` argument using a somewhat confusing syntax (a vector of 2, 3 or 4 numeric values), it’s now possible to use the user-friendly (and documented) `expand_scale()` function. This commit also cleans up the documentation related to the `expand` argument, which was duplicated in several functions.
The documentation for one of the functions had a no-breaking space, (between a number and the word ‘units’), which caused R CMD check to complain about ‘non-ASCII input and no declared encoding’. This adds a character encoding declaration of UTF-8 to the DESCRIPTION file to fix this problem.
@hadley, I have now added an One thing I’m not entirely happy with regarding this patch, is that the defaults for
to the more user-friendly
since |
Could this also be implemented for area plots, since especially when using a color scheme with a light color at the bottom it gives the impression of a much bigger area and additionally it kind of wastes space in a publications.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of tiny style issues and it'll be ready to merge 😄
R/utilities.r
Outdated
stopifnot(is.numeric(mult) && is.numeric(add)) | ||
stopifnot((length(mult) %in% 1:2) && (length(add) %in% 1:2)) | ||
|
||
mult = rep(mult, length.out = 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please use <-
here
R/utilities.r
Outdated
|
||
mult = rep(mult, length.out = 2) | ||
add = rep(add, length.out = 2) | ||
expand = c(mult[1], add[1], mult[2], add[2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for expand
variable
@hannes101 This feature works with scales in general, so yes, it should work with area plots too. |
@hadley I’ve now fixed the issues you mentioned and updated the PR to cleanly apply to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One tiny last change and we can merge 😄
@@ -172,6 +172,76 @@ rescale01 <- function(x) { | |||
(x - rng[1]) / (rng[2] - rng[1]) | |||
} | |||
|
|||
#' Similar to expand_range(), but taking a vector ‘expand’ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's @noRd
this
Thanks for all your hard work on this! |
…tidyverse#1805) * Allow separate expansion values for lower and upper range limits. The `expand` argument for `scale_*_continuous()` and `scale_*_discrete()` now accepts separate expansion constants for the lower and upper range limits. This is useful for creating bar charts where the bottom of the bars are flush with the x axis but the bars still have some (automatically calculated amount of) space above them: ```R ggplot(mtcars) + geom_bar(aes(x = factor(cyl))) + scale_y_continuous(expand = c(0, 0, 0.1, 0)) ``` It can also be useful for line charts, e.g. for counts over time, where one wants to have a ’hard’ lower limit of y = 0, but leave the upper limit unspecified (and perhaps differing between panels), but with some extra space above the highest point on the line. (With symmetrical limits, the extra space above the highest point could cause the lower limit to be negative.) The syntax for the multiplicative and additive expansion constants has been changed from `c(m, a)` to `c(m_lower, a_lower, m_uppper, a_upper)`. The old syntax will still work, as length 2 vectors `c(m, a)` are expanded to `c(m, a, m, a)` and length 3 vectors are expanded from `c(m1, a1, m2)` to `c(m1, a2, m2, a1)`. (@huftis, tidyverse#1669) * Added `expand_scale()` function for easier generation of scale expansion vectors. Instead of having to manually specify an `expand` argument using a somewhat confusing syntax (a vector of 2, 3 or 4 numeric values), it’s now possible to use the user-friendly (and documented) `expand_scale()` function. This commit also cleans up the documentation related to the `expand` argument, which was duplicated in several functions. * Added UTF-8 character encoding declaration to DESCRIPTION. The documentation for one of the functions had a no-breaking space, (between a number and the word ‘units’), which caused R CMD check to complain about ‘non-ASCII input and no declared encoding’. This adds a character encoding declaration of UTF-8 to the DESCRIPTION file to fix this problem. * Fixed some style issues. * Updated and regenerated documentation. * Specify character encoding used for documentation. * Minor grammar improvement in documentation. * Don’t generate documentation for internal function expand_range4().
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/ |
Fixes issue #1669.
The
expand
argument forscale_*_continuous()
andscale_*_discrete()
now accepts separate expansion constants for the lower and upper range limits.This is useful for creating bar charts where the bottom of the bars are flush with the x axis but the bars still have some (automatically calculated amount of) space above them:
It can also be useful for line charts, e.g. for counts over time, where one wants to have a ’hard’ lower limit of y = 0, but leave the upper limit unspecified (and perhaps differing between panels), but with some extra space above the highest point on the line. (With symmetrical limits, the extra space above the highest point could cause the lower limit to be negative.)
The syntax for the multiplicative and additive expansion constants has been changed from
c(m, a)
toc(m_lower, a_lower, m_uppper, a_upper)
. The old syntax will still work, as length 2 vectorsc(m, a)
are expanded toc(m, a, m, a)
and length 3 vectors are expanded fromc(m1, a1, m2)
toc(m1, a2, m2, a1)
.Note that by default, all graphs should exactly look like they do with the current version of
ggplot2
.Some examples (only meant to illustrate the functionality – several don’t make much sense from a graphical point of view):