-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
geom_bar()/geom_col() erroneously warn that they ignore width aesthetic #3142
Comments
Currently, Line 130 in 43dcd63
|
elsewhere (e.g. boxplot), Line 162 in 03bd946
|
|
Isn't |
Sorry, I was confused. Now I come to think the varying widths of Lines 40 to 46 in 43dcd63
In |
But, in terms of the interface (I don't mean the current behaviour is semantically correct), I'm wondering why |
Oh, this last example reminds me of the need for varying width.
|
Here's my understanding. Is this correct?
|
Just a comment in passing: If For the standard bar-chart with "meaningless" width, I would argue that the current default width of library("reprex")
library("ggplot2")
ggplot(mtcars, aes(x = gear)) + geom_bar() ggplot(mtcars, aes(x = gear)) + geom_bar(width = 0.5) ggplot(mtcars, aes(x = gear)) + geom_bar(width = 0.25) Created on 2019-02-18 by the reprex package (v0.2.1) |
Just to comment here that allowing 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(ggplot2)
d <- mtcars %>%
group_by(am) %>%
count(cyl) %>%
mutate(total = sum(n),
norm_n = n / total)
(p <- ggplot(d, aes(0, norm_n, fill = factor(cyl))) +
facet_grid(cols = vars(am)) +
geom_col(aes(width = total), position = position_stack()))
#> Warning: Ignoring unknown aesthetics: width p + aes(x = total/2) + coord_polar("y") Created on 2021-08-12 by the reprex package (v2.0.0) |
Variable width is useful for bar charts by month, to prevent the bars from overlapping. Especially if you want no gaps between the bars, but also because you'll get inconsistent gaps otherwise. You can hack it by making the dates a factor instead, but then you need to do much more work to get a nice date axis. library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
set.seed(1)
df <- tibble(
date = seq.Date(ymd("2020-01-01"), ymd("2020-12-01"), by = "1 month"),
quantity = sample(20:100, 12),
ndays = days_in_month(date) # width for different months
) |>
mutate(date = date + ndays / 2) # reposition to fix the overlaps
df
#> # A tibble: 12 × 3
#> date quantity ndays
#> <date> <int> <int>
#> 1 2020-01-16 87 31
#> 2 2020-02-15 58 29
#> 3 2020-03-16 20 31
#> 4 2020-04-16 53 30
#> 5 2020-05-16 62 31
#> 6 2020-06-16 33 30
#> 7 2020-07-16 78 31
#> 8 2020-08-16 70 31
#> 9 2020-09-16 40 30
#> 10 2020-10-16 73 31
#> 11 2020-11-16 26 30
#> 12 2020-12-16 56 31
df |>
ggplot() +
geom_col(aes(date, quantity, width = ndays), alpha = 0.7)
#> Warning in geom_col(aes(date, quantity, width = ndays), alpha = 0.7): Ignoring
#> unknown aesthetics: width Created on 2023-11-30 with reprex v2.0.2 |
I understand that we do not want to encourage bar charts with variable widths, however I do think enforcing this is causing us more pain than gain. I'd like to challenge some points in favour of not recognising
Not really. It throws warnings about being ignored, while it is being used.
While the hack works to recognise the parameter, we wouldn't need the hack at all if it were a proper aesthetic.
Ideally, the geom shouldn't care whence the I'd also like to re-iterate some points in favour of
In summary, the main argument against |
@teunbrand Let me go back on my argument from six years ago. While I still think one has to be careful with variable widths in a plot, I also these days believe plotting software should be maximally flexible and not impose specific design philosophies on their users. So unless there's a good technical reason not to have |
Thanks Claus, it seems we are in alignment then over this. I didn't mean to single out your arguments (and I'm sorry if it appeared that way). I just felt that this issue was stuck in a weird place of being acknowledged and having proposed solutions, but being dormant for a while. My arguing hopefully would get folks on board with the 'width as aesthetic' approach, so we can move forward on this issue. |
No worries, I didn't feel singled out. In fact, I was surprised by my own comment from 2019 as today I don't think I would write it. (I came here thinking: let me argue in favor of |
I apreciate @teunbrand's take on the subject, but -- and I may well have missed something -- I am not sure the proposed fix works as intended. See comment at #5807. A typical example of valid use of bar width is for multiplicative units, such as average price x number of units to get a transaction volume displayed as area. Such graphs are quite common in physics / engineering / climate science, etc. |
geom_bar()
andgeom_col()
let you specify awidth
aesthetic to control the width of the bars.The behavior is as expected, but it generates an erroneous warning "Warning: Ignoring unknown aesthetics: width".
width
isn't listed in the aesthetics section of?geom_bar
, so it appears that this is an unofficial behavior.Created on 2019-02-13 by the reprex package (v0.2.0).
Similar closed issues.
The text was updated successfully, but these errors were encountered: