ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Consolidate scale definition of position aesthetics

Open teunbrand opened this issue 1 year ago • 8 comments
trafficstars

This PR aims to fix #3342 and fix #4966.

Briefly, all position scales now use the definitions of x- and y-aesthetics from the gglobal_global environment. This ensures that rarer position aesthetics, like xintercept, contribute to the scale limits and are transformed by the scale.

I have been forewarned that this might lead to unexpected issues, but I don't foresee any at the moment. It might lead to some visual changes, but I'd argue they might be intended.

Reprex from #4966:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
library(lubridate)

set.seed(1234)
df = tibble(
  x = as.Date(0:19, origin = ymd("2022-01-01")), 
  y = rnorm(20)
)
df |>
  ggplot(aes(x, y)) + 
  geom_point() + 
  geom_hline(yintercept = 5) +                 
  geom_vline(xintercept = ymd("2022-01-10")) +
  geom_vline(xintercept = ymd("2022-03-01"))

Created on 2024-01-12 with reprex v2.0.2

teunbrand avatar Jan 12 '24 14:01 teunbrand

Are there any plans to put the solution regarding this issue into the next release? It's really annoying that vertical lines plotted far outside the date limits are not considered.

michaelgrund avatar Feb 29 '24 14:02 michaelgrund

It's really annoying that vertical lines plotted far outside the date limits are not considered.

I totally agree with you there.

We want the next release to be a low-risk hotfix where we mostly fix mistakes from 3.5.0 and not put in something disruptive. This PR has a non-zero risk of breaking the assumption of some packages that depend on ggplot2, so I think it'd be wise to postpone this a little bit further (or run revdepcheck for this PR). Any thoughts @thomasp85?

teunbrand avatar Feb 29 '24 14:02 teunbrand

yes, this will be postponed

thomasp85 avatar Mar 05 '24 07:03 thomasp85

After a lot of digging I found that the addition of "intercept" to the list of positional aesthetics was the cause of the troubles I remembered. Since this is not touched here I think it's safe. Will run revdepcheck regardless

thomasp85 avatar May 15 '24 10:05 thomasp85

Bunch of revue-check failures. I'm pretty sure that most or all are unrelated to this but please sift through them

thomasp85 avatar May 16 '24 06:05 thomasp85

I can see the irony in some of the revdep failures. Basically, people have found workarounds for the problem that date scales don't accept {x/y}intercept aesthetics. However, this PR now breaks those workarounds by making it easier.

For example this now correctly throws the error that it will not accept any non-dates.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

# Analogous to current workarounds
ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  geom_vline(xintercept = as.numeric(as.Date("1983-01-31")))
#> Error in `transformation$transform()` at ggplot2/R/scale-.R:628:3:
#> ! `transform_date()` works with objects of class <Date> only

Whereas now the solution is to simply drop the as.numeric() workaround.

ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  geom_vline(xintercept = as.Date("1983-01-31"))

Created on 2024-05-21 with reprex v2.1.0

teunbrand avatar May 21 '24 14:05 teunbrand

TODO: try to catch these situations, convert to numeric with warning

teunbrand avatar Jun 04 '24 09:06 teunbrand

Numeric input now throws warnings instead of errors:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  geom_vline(xintercept = as.numeric(as.Date("1983-01-31")))
#> Warning in scale_x_date(): A <numeric> value was passed to a Date scale.
#> ℹ The value was converted to a <Date> object.

ggplot(economics, aes(as.POSIXct(date), unemploy)) +
  geom_line() +
  geom_vline(xintercept = as.numeric(as.POSIXct("1983-01-31")))
#> Warning in scale_x_datetime(): A <numeric> value was passed to a Datetime scale.
#> ℹ The value was converted to a <POSIXt/POSIXct> object.

Created on 2024-06-04 with reprex v2.1.0

teunbrand avatar Jun 04 '24 10:06 teunbrand