ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Feat/issue 4899 simplify alignment for column geoms

Open wurli opened this issue 2 years ago • 4 comments

This PR adds a just argument to geom_col() and geom_bar() (#4899) allowing a user to more easily change the alignment of columns. In particular, it makes it easy to align the sides of columns with axis breaks in cases where the resolution() of the data is not trivial to calculate.

  • just is used rather than hjust. This has a downside that it breaks with the hjust/vjust pattern used elsewhere in ggplot2, however it seems preferable since hjust would necessitate vjust for horizontal columns, which would raise difficult questions about whether to modify other behaviour, e.g. of has_flipped_aes().
  • There is a small improvement to the documentation for the width argument - resolution of the data now reads as resolution() of the data to more clearly hint at what's happening under the hood
  • This PR does not (as yet) include the extra aesthetics xmin and ymin for geom_col()

Here's how to use the just argument:

devtools::load_all()
#> i Loading ggplot2

df <- data.frame(
  x = 1:3,
  y = 1:3
)

ggplot(df, aes(y, x)) +
  geom_col(just = 1)

Created on 2022-07-26 by the reprex package (v2.0.1)

wurli avatar Jul 26 '22 13:07 wurli

Thanks. Could you avoid using the Windows line ending? If you are using RStudio, you can set it here (you might also need to review your Git settings as Git might convert the line endings on committing).

image

yutannihilation avatar Jul 26 '22 14:07 yutannihilation

Thanks. Could you avoid using the Windows line ending? If you are using RStudio, you can set it here (you might also need to review your Git settings as Git might convert the line endings on committing).

Thanks, done now. I was puzzling about why the diff was showing all files as changed!

wurli avatar Jul 26 '22 15:07 wurli

Thanks, looks almost good. Random comments:

  • I think we should add at least one example to show how just works briefly.
  • When used with some positions (e.g. position_dodge()), non-default just might result in a bit weird look. I'm wondering if we can add some friendly note about this.

Both of these are now complete. The friendly note is in the documentation for the just argument itself, but may be more appropriate in the example. Not a big deal either way though 😄 Thanks!

wurli avatar Jul 27 '22 09:07 wurli

Do you think the whole setup_data field for geom_col() and geom_bar() should have its own named function (e.g. setup_col_data()) to avoid code duplication btw?

I can't remember the reason why GeomBar and GeomCol are implemented separately, but, as now they are just duplicate. There's a plan to remove it (https://github.com/tidyverse/ggplot2/issues/3798), but, for now, could you just leave them duplicate? I think we should do one by one..

yutannihilation avatar Jul 27 '22 12:07 yutannihilation

LGTM

thomasp85 avatar Aug 23 '22 11:08 thomasp85

Thanks for reviewing!

yutannihilation avatar Aug 23 '22 11:08 yutannihilation