-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add asymmetrical expand
argument to continuous and discrete scales
#1669
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
Comments
Have a look at the
|
@steveharoz
|
This doesn’t seem too difficult to add. I’ll try to create a patch and a pull request. |
The `expand` argument for `scale_*_continuous()` and `scale_*_discrete()` now accepts separate expansion constants for the lower and upper range limits. This makes it much easier to create 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 = cyl)) + scale_y_continuous(expand = c(0, 0, 0.05, 0)) ``` 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)
Hmmm, I'm not convinced I want this. Would you mind including a couple of before and after plots? |
@hadley Sure. First, note that this does not change the default look of ggplot2 graphs at all. So a simple before and after plot would look identical. It gives the user the option of asymmetrical automatic range expansion for the x and y scales. This is most useful for bar charts (but there may be other uses). In almost all programs, the bars are attached to an axis, with no gap. Illustration: https://www.google.no/search?q=bar+chart&tbm=isch However, in ggplot2 there is a gap, making the bars hover above the x axis: or the y axis: One can use the One can use a This pull request adds a feature for asymmetric range expansion. By default (and if one only specifies a length 2 expansion), everything works as before, with symmetric range expansion. But one can now also specify separate expansion constants for the lower and upper limits. This can be used to remove the gap in bar charts (while still leaving some space above / to the right of the bars): I think many will find this a useful addition to ggplot2. Evidence:
(The last link illustrates a use case for a different type of graph than a bar chart.) |
Hmmmm, that looks really weird to my eyes. I think ideally you want to have about the same margin in each dimension. The default ggplot2 margins aren't perfect (because of the additive/multiplicative) nature, but this makes it even worse. |
I personally think it has some merit for bar charts, but agree that it looks weird with the default theme. A style where only the y-axis is drawn and with a white background serves it better... |
Yes, that wasn’t an attempt to make a ‘perfect bar chart’ at all. (Firstly, I wouldn’t use the default theme, but typically one with a transparent background. And I would get rid of the tick marks. And the horizontal grid lines. And add some (symmetrical!) space (using |
@hadley Can we either close or discuss the preferred API for setting asymmetric expansion. I'm personally for this feature for the single use case of bar charts (have personally done it manually with expand_limits a couple of times), but it is added complexity... |
It still looks weird to me, but I'd accept a PR that fixed it. @huftis would you be willing take another shot at it? To produce cleaner code, you'll need to setup a basic system of S3 classes that insulate the complexity of the new approach into a central location. |
@hadley Sure, I’ll take a new look at creating a PR. |
@huftis are you still interested? We'll be making a release candidate of ggplot2 in one week, so it'll need to be done by then if you want it in the next version of ggplot2. |
Yes, still interested. Been a bit busy lately, but I’ll try to find time to get it done in the coming week. |
@hadley I have a simplified (mostly) working version, but I’m really puzzled by some existing code in range(
expand_range(c_range, expand[1], 0 , 1),
expand_range(c(1, length(d_range)), 0, expand[2], 1)
) My understanding of this is: First an expanded range is calculated for the continuous scale, but only using the multiplicative constant (i.e. setting the additive constant to zero). And then an expanded range is calculated for the discrete scale, but only using the additive constant (i.e. setting the multiplicative constant to zero). And then the interval enclosing both these ranges is returned. What is the logic behind this? |
Try to update to the latest version - this should have been fixed... |
@thomasp85 I am using the latest version. That’s where I copied this piece of code from. |
Oh, sorry - was thinking about something else slightly related (a few lines up) |
Actually it is related - it is an oversight from when we fixed the other... Thanks for catching that |
I think I understand it a bit better, but I still think it looks strange. For ggplot(mtcars, aes(x = factor(cyl))) + geom_bar() there will be three vertical bars, and Now, internally, this will be a discrete scale, so will have the default expansion of For the continuous part, the calculation is I guess this can be useful for data like ggplot(mtcars, aes(factor(cyl), mpg)) + geom_jitter() where ggplot(mtcars, aes(factor(cyl), mpg)) + geom_jitter(width=10) I guess what you really want is to have separate However, if one wants to use a multiplicative constant for the continuous part (but no additive constant) and an additive constant for the discrete part (but no multiplicative constant), one can use the (undocumented) workaround |
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)
I’ve now added a new pull request. The code has been simplified, and all the calculations now happen in a |
Has this somehow been implemented yet? I generally agree with @huftis on the usefulness in bar plots, but also with @hadley on the inelegance of the solution provided here.... |
@dzion I haven’t have time to implement it yet, but will do so (though obviously not in time for the next ggplot2 release). It will probably have the syntax |
@huftis Don't you feel that simply adding the |
I would suggest adding the parameters |
We already have a reasonable API (that I like) in the PR, so I don't think we need to discuss it further. |
Ok - didn't know the API was settled. I'll show myself out then🙂 |
I think similar issues arise with area plots. They should sit on the horizontal axis and kiss the left and right borders. One can do this with the current expand(), but then the area plots bump their heads. |
…#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, #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().
…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().
Add a blank geom_text layer to expand the upper limit of barchart. For example:
|
This is a feature request for an asymmetrical
expand
argument to thescale_continuous
andscale_discrete
scales. A typical use case would be for bar plots that touch the x axis but still have some space above them. For examplehas some space above the bars, but also an annoying spaces below it. Now,
gets rid of the space below the bars, but also the space above the bars. To keep the space above the bars, one has to use a hack with a manually calculated value:
(And this doesn’t work well with more complicated plots, e.g. involving facets with
free_y
&c.)Suggested feature/syntax: Change the
expand
argument fromto
where
a
is the multiplier for the lower limit,b
is the additive term for the lower limit, andc
andd
are the corresponding multiplier and additive term for the upper limit. Ifc
is missing, use the value ofa
, and ifd
is missing, use the value ofb
. This way, all old code would continue to work.See http://stackoverflow.com/questions/34623780/asymmetric-expansion-of-limits-ggplot2-2-0 for more information. This post also has suggested code for an asymmetrical
expand
feature, but this does not work for complicated facet grids, nor withcoord_flip()
.The text was updated successfully, but these errors were encountered: