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
Unexpected (?) orientation with geom_col #3932
Comments
Thanks, confirmed. This is because Lines 600 to 602 in bca6105
|
Hmm interesting, thanks for the insight. I don't really have strong feelings either way (after writing the below.... maybe I do :) ). I'm not sure why tibble::tribble(
~x, ~y,
0.05, 303L,
0.1, 202L,
0.15, 134L,
0.2, 67L,
0.25, 29L,
0.3, 10L
) In this instance while In fact, having looked at the source code you just linked, it seems the following lines do try to figure this out: Lines 631 to 632 in bca6105
This seems to fail though with non-integer numbers (like mine): x <- seq(0.05, 0.3, 0.05)
x_diff <- diff(sort(x))
x_diff <- x_diff[x_diff != 0]
all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps)
#> [1] FALSE
# Integers
x <- seq(1, 6, 1)
x_diff <- diff(sort(x))
x_diff <- x_diff[x_diff != 0]
all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps)
#> [1] TRUE Could this be fixed by introducing some tolerance similiar to x <- seq(0.05, 0.3, 0.05)
x_diff <- diff(sort(x))
x_diff <- x_diff[x_diff != 0]
all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps^0.5)
#> [1] TRUE |
This example on SO seems to choose non-deterministically: https://stackoverflow.com/q/61235860/2554330. I saw both of the plots in |
Is that SO yours? I have just tried executing/knitting that code many times and I consistently only get the incorrect orientiation (the output matched the second figure) or the correct orientiation if I add From what I can tell, it seems that the example in the SO post suffers the same problem as my code above: x <- c(-9.4, -9.3, -9.2, -9, -8.9, -8.8, -8.7, -8.5, -8.4, -8.3, 0)
x_diff <- diff(sort(x))
x_diff <- x_diff[x_diff != 0]
(x_diff / min(x_diff)) %% 1
#> [1] 0.000000e+00 1.776357e-14 0.000000e+00 0.000000e+00 0.000000e+00
#> [6] 1.776357e-14 0.000000e+00 0.000000e+00 0.000000e+00 2.984279e-13
all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps)
#> [1] FALSE
all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps^0.5)
#> [1] TRUE |
I thought I did see both, but now when I re-run it, I can't get the "x" orientation again. What might have happened is that I accidentally edited the data to contain non-integer My taste would run to a much simpler rule: if one axis is a factor, that determines orientation. If not, default to "x". |
The SO says
So, I'm not sure if this is the same issue as you encountered.
Yeah, true. The difficulty is that the current logic determines the orientation at the stage where there's no information about the type of axis... |
It appears that repeated values of y will trigger df <- data.frame(
x = seq(from = 100, to = 300, length.out = 20),
y = (rep(6.5:10.5, 4)),
z = factor(c(rep(1, 5), rep(2, 5), rep(3, 5), rep(4, 5)))
)
ggplot2::ggplot(df, ggplot2::aes(x, y, fill = z)) +
ggplot2::geom_col(position = "dodge") Created on 2020-04-24 by the reprex package (v0.3.0) According to the documentation for geom_col, this appears to be one of the rare events where determining orientation from aesthetic mapping fails. |
I'm using ggplot2 v3.3.0.9000. The orientation of the
geom_col
bars is not correct when values of x are numeric and less than 1. Specifyingorientation = "x"
does what I would expect should be default behaviour. From reading the documentation (https://ggplot2.tidyverse.org/reference/geom_bar.html), it does statex
is the default oreintation but that doesn't seem to be the case here.The text was updated successfully, but these errors were encountered: