Skip to content

ggplot breaks when facet variable is POSIXct with tz attribute specified #4175

Closed

Description

@edzer

This came up here, and I think I can reduce it to the presence of a time zone attribute in a POSIXct vector. Not sure if this is a vctrs issue or ggplot2, but this is where it showed up.

Minimal reprex:

library(ggplot2)
x = data.frame(x = 1:6, y = 6:1, time = strptime("2020-09-14",format="%Y-%m-%d") + 1:6)
ggplot(x) + geom_point(aes(x=x, y=y)) +facet_wrap(~time) # OK
x = data.frame(x = 1:6, y = 6:1, time = strptime("2020-09-14",format="%Y-%m-%d",tz="UTC") + 1:6)
ggplot(x) + geom_point(aes(x=x, y=y)) +facet_wrap(~time)
# Error: `scale_id` must not be `NA`
# Backtrace:
#     █
#  1. ├─(function (x, ...) ...
#  2. └─ggplot2:::print.ggplot(x)
#  3.   ├─ggplot2::ggplot_build(x)
#  4.   └─ggplot2:::ggplot_build.ggplot(x)
#  5.     └─layout$train_position(data, scale_x(), scale_y())
#  6.       └─ggplot2:::f(..., self = self)
#  7.         └─self$facet$train_scales(...)
#  8.           └─ggplot2:::f(...)
#  9.             └─ggplot2:::scale_apply(layer_data, x_vars, "train", SCALE_X, x_scales)
# Execution halted

Activity

  1. yutannihilation commented on Aug 18, 2020

    @yutannihilation
    Member

    Thanks, confirmed.

    On this line, facet_vals and layout doesn't match, since the latter got stripped its timezone. This results in missing scale_id.

    keys <- join_keys(facet_vals, layout, by = names(vars))

    It seems the problem comes from rbind_dfs(), which is used here:

    self$layout <- self$facet$compute_layout(data, self$facet_params)

    x = data.frame(x = strptime("2020-09-14",format="%Y-%m-%d",tz="UTC") + 1:2)
    x
    #>                     x
    #> 1 2020-09-14 00:00:01
    #> 2 2020-09-14 00:00:02
    ggplot2:::rbind_dfs(list(x))
    #> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
    #> when loading 'dplyr'
    #>                     x
    #> 1 2020-09-13 15:00:01
    #> 2 2020-09-13 15:00:02

    Created on 2020-08-18 by the reprex package (v0.3.0)

  2. thomasp85 commented on Aug 31, 2020

    @thomasp85
    Member

    hopefully this will all be fixed when we begin using vctrs

  3. added
    bug
    on Aug 31, 2020
  4. added a commit that references this issue on Feb 6, 2021
    2a646c6
  5. removed this from the ggplot2 3.4.0 milestone on May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfacets 💎

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions