wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
701 stars 84 forks source link

Cowplot breaks dates? #196

Closed senanu closed 11 months ago

senanu commented 1 year ago

First, thank you for such a useful package! However, it seems that cowplot breaks the dates on an X-axis. I have seen on Stackoverflow that this error is generated when the data type for the axis isn't a proper date or when the axis is manipulated by xmin() etc. However, in the reprex below, I think none of that is the case, but it still doesn't work properly. Is there a solution or workaround?

Thank you!

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(reprex)
x <- as.Date(c("2023-01-01", "2023-01-01", "2022-06-06"))
df <- tibble::tibble(x)
myplot <- df %>% ggplot(aes(x = x)) +
  geom_bar()
myplot

myplot + cowplot::draw_figure_label("A", position = 'top.left')
#> Error: Invalid input: date_trans works with objects of class Date only

Created on 2023-09-07 with reprex v2.0.2

Doubt-0KB commented 11 months ago

It seems that cowplot::draw_figure_label() is designed to be applied to cowplots. So I think that you can add the label to the plot by changing your code like this.

library(ggplot2)
library(dplyr)
x <- as.Date(c("2023-01-01", "2023-01-01", "2022-06-06"))
df <- tibble::tibble(x)
myplot <- cowplot::plot_grid(df %>% ggplot(aes(x = x)) + geom_bar())
myplot + cowplot::draw_figure_label("A", position = 'top.left')

image

senanu commented 11 months ago

Great! That works well and makes sense. Thank you for your time.