Skip to content

scale_x_binned() vs. POSIXct #4217

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

Closed
domq opened this issue Oct 1, 2020 · 1 comment · Fixed by #5103
Closed

scale_x_binned() vs. POSIXct #4217

domq opened this issue Oct 1, 2020 · 1 comment · Fixed by #5103
Labels
feature a feature request or enhancement scales 🐍

Comments

@domq
Copy link

domq commented Oct 1, 2020

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.

@domq
Copy link
Author

domq commented Oct 1, 2020

Also,

lakers %>% mutate(date = as.Date(ymd(date))) %>%
    ggplot(aes(x = date, fill = game_type, width = 0.1)) +
    geom_bar(position = "dodge") +
    scale_x_binned(trans = "date")
Error: Invalid input: date_trans works with objects of class Date only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants