微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误:当我使用 gg4xh 库中的 facetted_pos_scales 时输入无效

如何解决错误:当我使用 gg4xh 库中的 facetted_pos_scales 时输入无效

假设我的数据具有以下结构。

library(ggplot2)
library(ggh4x)
VARIANCE<-structure(list(MODEL = c("RECOV","RECOV","SHAR","SHAR"),name = structure(c(1L,2L,3L,4L,5L,11L,12L,13L,14L,15L,6L,7L,8L,9L,10L,1L,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,14822,14823,14823),.Dim = c(90L,1L),.Dimnames = list(NULL,NULL))),row.names = c(NA,-90L),class = c("tbl_df","tbl","data.frame"))

有了这些信息,我想使用下面的代码生成一个绘图。

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

结果几乎是完美的。唯一的错误是 Y 轴,当我使用我的代码时,Y 轴不显示日期而是数字。我知道我可以使用 as.Date(VARIANCE$DATE,origin="1970-01-01") 纠正这个问题但是,如果我应用这行代码然后我运行我的 ggplot 代码,我会收到以下错误 Error: Invalid input: date_trans works with objects of class Date only

解决方法

临时解决方法:只需将转换函数从 scales::date_trans() 更改为不要对日期挑剔。我之前complained了解过这些类型的日期(时间)刻度限制。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。