ggplot2中的scale_x_labels出错

前端之家收集整理的这篇文章主要介绍了ggplot2中的scale_x_labels出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用ggplot2在 this book中重新创建图表.
我使用的数据位于dput的帖子底部.它可以从出版商的网站上获得.我正在重新创建第12页的时间序列图.

这是我正在使用的代码

y_min <- min(chartout$LCL)
y_max <- max(chartout$UCL)

ggplot() + 
        geom_line(mapping=aes(x=Date,y=Rate),data=chartout,size=1,alpha=0.5) +
        geom_point(data = chartout,mapping=aes(x=Date,size=3) +
        expand_limits(y = c(y_min,y_max)) +
        geom_line(mapping=aes(x=Date,y=UCL),colour='red',alpha=0.5) + 
        geom_line(mapping=aes(x=Date,y=LCL),alpha=0.5) +
        scale_x_date(labels = date_format("%m %d")) +
        theme_bw()

问题是我遇到了以下错误

Error: Invalid input: date_trans works with objects of class Date only

我已经看过this question,但你可以从dput的结构中看到Date是一个Date格式.我知道这是因为我使用lubridate转换它.

任何想法如何解决这一问题?

这里的输出输出

chartout <- structure(list(Rate = c(0.2558,0.2351,0.4804,0.2357,0.2262,0.0433,0.0839,0.0422,0.2569,0.1283,0.045,0.2418,0.3373,0.3214,0.3026,0.1307,0.2986,0.0431,0.1259,0.3339,0.0432,0.2083,0.3861,0.0461,0.4428,0.1368,0.3212,0.1766,0.1643,0.2503,0.2033,0.1199,0.2084,0.161,0.3729,0.4103,0.4315,0.2195,0.3619,0.1698,0.2368,0.3717,0.1943,0.3199,0.2495,0.3587,0.1673,0.3028,0.2572,0.2976,0.4033,0.2813,0.3966,0.1609),Mean = c(0.2411,0.2411,0.2411
),Date = structure(c(1072915200,1075593600,1078099200,1080777600,1083369600,1086048000,1088640000,1091318400,1093996800,1096588800,1099267200,1101859200,1104537600,1107216000,1109635200,1112313600,1114905600,1117584000,1120176000,1122854400,1125532800,1128124800,1130803200,1133395200,1136073600,1138752000,1141171200,1143849600,1146441600,1149120000,1151712000,1154390400,1157068800,1159660800,1162339200,1164931200,1167609600,1170288000,1172707200,1175385600,1177977600,1180656000,1183248000,1185926400,1188604800,1191196800,1193875200,1196467200,1199145600,1201824000,1204329600,12070
  

  

Error: Invalid input: date_trans works with objects of class Date only

,1209600000,1212278400),class = c("POSIXct","POSIXt"),tzone = "UTC"),UCL = c(0.6601,0.6397,0.6227,0.6403,0.6306,0.6208,0.6137,0.6151,0.6183,0.618,0.6294,0.6464,0.6456,0.6341,0.6204,0.6221,0.6174,0.6197,0.6139,0.6126,0.6205,0.6122,0.6187,0.6351,0.6258,0.6324,0.6042,0.6252,0.6091,0.6125,0.6069,0.6032,0.6124,0.6047,0.611,0.62,0.6238,0.6045,0.6163,0.6006,0.6103,0.5973,0.6033,0.6118,0.6026,0.6131,0.6206,0.6185,0.6167,0.6051,0.6043,0.6016,0.6046),LCL = c(0L,0L,0L)),.Names = c("Rate","Mean","Date","UCL","LCL"),row.names = c(NA,-54L),class = "data.frame")

解决方法

你应该使用scale_x_datetime,因为你有POSIXct:
scale_x_datetime(labels=date_format("%Y %H:%M"))

或者将Date变量转换为Date(没有时间部分)

chartout$Date <- as.Date(chartout$Date)

我会使用这种格式:

scale_x_date(labels = date_format("%m %b")) +
原文链接:https://www.f2er.com/css/214891.html

猜你在找的CSS相关文章