Eve Chanatasig
Eve Chanatasig

Reputation: 397

Error: Invalid input when I work with facetted_pos_scales from gg4xh library

Let's suppose that my data has the following structure.

library(ggplot2)
library(ggh4x)
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"))

With this information I want to generate a Plot using the code below.

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))

The result is almost perfect. The only error is the Y-axis, when i use my code the y-axis does not present dates but numbers. I know that i could correct this issue using as.Date(VARIANCE$DATE ,origin="1970-01-01") But, if I apply this line of code and then i run my ggplot code i get the following error Error: Invalid input: date_trans works with objects of class Date only

Upvotes: 0

Views: 455

Answers (1)

teunbrand
teunbrand

Reputation: 37903

Temporary workaround: just change the transform function from the scales::date_trans() to not be fussy about dates. I've complained about these types of restrictions on date(time) scales before.

library(ggplot2)
library(ggh4x)

dtrans <- scales::date_trans()
dtrans$transform <- function(x) {unclass(x)}

# VARIANCE assumed to be from example
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)
  )+
  scale_x_continuous(trans = dtrans) +
  theme_test() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

Upvotes: 3

Related Questions