ggplot2中的scale_x_labels错误 [英] error with scale_x_labels in ggplot2

查看:223
本文介绍了ggplot2中的scale_x_labels错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新创建这本书中的图表,使用ggplot2。
我正在使用的数据是从 dput 的帖子的底部。可从发布商的网站获得。我正在重新创建第12页的时间序列图。

I am trying to recreate the charts in this book using ggplot2. The data I am using is at the bottom of the post from a dput. It is available from the publisher's website. I am recreating the time series plot on page 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,y=Rate), size=3) +
        expand_limits(y = c(y_min, y_max)) +
        geom_line(mapping=aes(x=Date, y=UCL), data=chartout, colour='red',  size=1, alpha=0.5) + 
        geom_line(mapping=aes(x=Date, y=LCL), data=chartout, colour='red' , size=1, alpha=0.5) +
        scale_x_date(labels = date_format("%m %d")) +
        theme_bw()

问题是我遇到以下错误:

The problem is that I get hit up with the following error:


错误:输入无效:date_trans与类Date的对象一起工作

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

我已经看到这个问题,但您可以从 dput 中的结构中看到日期是日期格式。我知道这是因为我使用 lubridate 转换它。

I have already seen this question but you can see from the structure in the dput that the Date is in a Date format. I know this because I converted it using lubridate.

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

Any ideas how to fix this?

dput output here:

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, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 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, 1207008000, 
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.6324, 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, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Rate", 
"Mean", "Date", "UCL", "LCL"), row.names = c(NA, -54L), class = "data.frame")


推荐答案

code> scale_x_datetime 因为你有 POSIXct

You should use scale_x_datetime since you have POSIXct :

 scale_x_datetime(labels=date_format("%Y %H:%M"))

或将您的Date变量转换为日期(无时间部分)

Or convert you Date variable to a Date ( without time part)

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

我会使用这种格式:

scale_x_date(labels = date_format("%m %b")) +

这篇关于ggplot2中的scale_x_labels错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆