Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

r - error with scale_x_labels in ggplot2

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.

Here is the code I am using:

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:

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

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use scale_x_datetime since you have POSIXct :

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

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

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

And I would use this format :

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...