Skip to content

scale_x_binned() vs. POSIXct #4217

Closed
@domq

Description

@domq

What I am trying to achieve:

Binned histograms rendered as Excel®-style grouped bar graphs, when the x axis is a timestamp.

This does exactly what I want when x is numeric:

ggplot(diamonds, aes(x = price, fill = cut)) + geom_bar(stat = "count", position = "dodge") + scale_x_binned()

image

The same approach doesn't work with POSIXct:

library(dplyr)
library(lubridate)
lakers %>% mutate(date = ymd(date)) %>%
ggplot(aes(x = date, fill = game_type)) + geom_bar(stat = "count", position = "dodge") + scale_x_binned()
Error: Binned scales only support continuous data

“Casting” the x into an integer is tantalizing close to what I want:

lakers %>% mutate(date = ymd(date)) %>%
ggplot(

    aes(x = int(date),  # Emphasis mine

        fill = game_type)) +
geom_bar(stat = "count", position = "dodge") + scale_x_binned()

image

... but of course, the labeling on the x axis could be better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions