teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
541 stars 33 forks source link

facetted_pos_scales does not work with Date objects #37

Closed Evelyn0 closed 3 years ago

Evelyn0 commented 3 years ago

Using the following time series data VARIANCE<-structure(list(MODEL = c("RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR"), name = structure(c(1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L), .Label = c("From others to NSW", "From others to QLD", "From others to SA", "From others to TAS", "From others to VIC", "Net NSW", "Net QLD", "Net SA", "Net TAS", "Net VIC", "To others from NSW", "To others from QLD", "To others from SA", "To others from TAS", "To others from VIC"), class = "factor"), value = c(4.39666597202896, 3.58762190497229, 4.30146313827031, 3.54812042879641, 4.43158464041545, 5.18520098870974, 3.27054464036042, 5.26640903744102, 3.10418977364104, 5.36337411384991, 0.788535016680784, -0.317077264611877, 0.964945899170713, -0.44393065515537, 0.931789473434457, 4.39509970212315, 3.59063595048007, 4.29749359454961, 3.5440048555318, 4.42683221820271, 5.18051503751689, 3.27437015775553, 5.25235072116727, 3.09408850709962, 5.35441540065628, 0.785415335393744, -0.316265792724537, 0.954857126617668, -0.449916348432182, 0.927583182453565, 4.3943104511415, 3.58990247949796, 4.29516509109662, 3.54864931461666, 4.42682253305903, 5.17365373149387, 3.26812914873157, 5.23933592999296, 3.08592441369308, 5.34801447553526, 0.779343280352365, -0.321773330766394, 0.944170838896331, -0.462724900923573, 0.921191942476225, 4.4395, 4.03075, 4.319, 4.0895, 4.40075, 5.50775, 2.56, 4.93, 3.08325, 5.244, 1.06825, -1.47075, 0.611, -1.00625, 0.84325, 4.442, 4.015, 4.324, 4.1045, 4.40475, 5.4955, 2.49525, 4.9655, 3.1115, 5.268, 1.0535, -1.51975, 0.6415, -0.993, 0.86325, 4.44475, 3.99575, 4.332, 4.1205, 4.411, 5.5015, 2.4375, 5.00625, 3.144, 5.2925, 1.05675, -1.55825, 0.67425, -0.9765, 0.8815), DATE = structure(c(14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823), .Dim = c(90L, 1L), .Dimnames = list(NULL, NULL))), row.names = c(NA, -90L), class = c("tbl_df", "tbl", "data.frame"))

This work

ggplot(VARIANCE, aes(y=value, x=DATE, color= MODEL))+ geom_line() + facet_wrap(vars(name), ncol = 5, scales = "free_y")+ facetted_pos_scales( y = rep(list( scale_y_continuous(limits = c(1.5, 5)), scale_y_continuous(limits = c(-2.2, 2)), scale_y_continuous(limits = c(1, 7)) ), each = 5) )+ theme_test() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

bug

but when i use that

as.Date(VARIANCE$DATE ,origin="1970-01-01")

I get the following error Error: Invalid input: date_trans works with objects of class Date only

teunbrand commented 3 years ago

Thanks for posting the issue!

Temporary workaround until I have time to fix this is posted as answer here.

Best wishes

teunbrand commented 3 years ago

Minimum reprex:

library(ggplot2)
library(ggh4x)

df <- data.frame(
  x = structure(c(14820), class = "Date"),
  y = c(1)
)

ggplot(df, aes(x, y)) +
  facetted_pos_scales()
#> Error: Invalid input: date_trans works with objects of class Date only

Created on 2021-04-06 by the reprex package (v1.0.0)

teunbrand commented 3 years ago

This should now work on the dev version:

VARIANCE<-structure(list(
  MODEL = c("RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", "RECOV", 
            "RECOV", "RECOV", "RECOV", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
            "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
            "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
            "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
            "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
            "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR"), 
  name = structure(c(1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 
                     9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 
                     7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 
                     15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 11L, 12L, 
                     13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
                     11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
                     4L, 5L, 11L, 12L, 13L, 14L, 15L, 6L, 7L, 8L, 9L, 10L), 
                   .Label = c("From others to NSW", "From others to QLD", 
                              "From others to SA", "From others to TAS", 
                              "From others to VIC", "Net NSW", "Net QLD", 
                              "Net SA", "Net TAS", "Net VIC", 
                              "To others from NSW", "To others from QLD", 
                              "To others from SA", "To others from TAS", 
                              "To others from VIC"), class = "factor"), 
  value = c(4.39666597202896, 3.58762190497229, 4.30146313827031, 
            3.54812042879641, 4.43158464041545, 5.18520098870974, 
            3.27054464036042, 5.26640903744102, 3.10418977364104, 
            5.36337411384991, 0.788535016680784, -0.317077264611877, 
            0.964945899170713, -0.44393065515537, 0.931789473434457, 
            .39509970212315, 3.59063595048007, 4.29749359454961, 
            3.5440048555318, 4.42683221820271, 5.18051503751689, 
            3.27437015775553, 5.25235072116727, 3.09408850709962, 
            5.35441540065628, 0.785415335393744, -0.316265792724537, 
            0.954857126617668, -0.449916348432182, 0.927583182453565, 
            4.3943104511415, 3.58990247949796, 4.29516509109662, 
            3.54864931461666, 4.42682253305903, 5.17365373149387, 
            3.26812914873157, 5.23933592999296, 3.08592441369308, 
            5.34801447553526, 0.779343280352365, -0.321773330766394, 
            0.944170838896331, -0.462724900923573, 0.921191942476225, 4.4395, 
            4.03075, 4.319, 4.0895, 4.40075, 5.50775, 2.56, 4.93, 3.08325, 
            5.244, 1.06825, -1.47075, 0.611, -1.00625, 0.84325, 4.442, 4.015, 
            4.324, 4.1045, 4.40475, 5.4955, 2.49525, 4.9655, 3.1115, 5.268, 
            1.0535, -1.51975, 0.6415, -0.993, 0.86325, 4.44475, 3.99575, 4.332, 
            4.1205, 4.411, 5.5015, 2.4375, 5.00625, 3.144, 5.2925, 1.05675, 
            -1.55825, 0.67425, -0.9765, 0.8815), 
  DATE = structure(c(14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 
                     14821, 14821, 14821, 14821, 14821, 14821, 14821, 14822, 
                     14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 
                     14822, 14822, 14822, 14822, 14822, 14822, 14823, 14823, 
                     14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 
                     14823, 14823, 14823, 14823, 14823, 14821, 14821, 14821, 
                     14821, 14821, 14821, 14821, 14821, 14821, 14821, 14821, 
                     14821, 14821, 14821, 14821, 14822, 14822, 14822, 14822, 
                     14822, 14822, 14822, 14822, 14822, 14822, 14822, 14822, 
                     14822, 14822, 14822, 14823, 14823, 14823, 14823, 14823, 
                     14823, 14823, 14823, 14823, 14823, 14823, 14823, 14823, 
                     14823, 14823), .Dim = c(90L, 1L), 
                   .Dimnames = list(NULL, NULL))), 
  row.names = c(NA, -90L), class = c("tbl_df", "tbl", "data.frame")
)

library(ggh4x)
#> Loading required package: ggplot2

ggplot(VARIANCE, aes(y=value, x=as.Date(DATE, origin = "1970-01-01"), color= MODEL))+ 
  geom_line() + 
  facet_wrap(vars(name), ncol = 5, scales = "free_y")+ 
  facetted_pos_scales( y = rep(list( scale_y_continuous(limits = c(1.5, 5)), 
                                     scale_y_continuous(limits = c(-2.2, 2)), 
                                     scale_y_continuous(limits = c(1, 7)) ), each = 5) )+ 
  theme_test() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

Created on 2021-04-10 by the reprex package (v1.0.0)