2013-12-23 1 views
6

ggplot2를 사용하여 this book의 차트를 다시 만들려고합니다. 사용중인 데이터는 dput의 게시물 하단에 있습니다. 게시자의 웹 사이트에서 제공됩니다. 내가 여기ggplot2의 scale_x_labels에 오류가 있습니다.

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

문제는 내가 다음과 같은 오류와 함께 맞고 있다는 것입니다 :

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

나는 이미 this question을 보았지만 dput의 구조에서 날짜가 날짜 형식임을 알 수 있습니다. 나는 이것을 lubridate을 사용하여 변환했기 때문에 이것을 안다.

이 문제를 해결하는 방법에 대한 아이디어가 있으십니까? 여기

dput 출력 : 당신이 POSIXct이 있기 때문에 당신은 scale_x_datetime를 사용해야합니다

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

답변

13

:

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

을 또는 변환 (시간 부분없이) 날짜에 변수를 날짜

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

그리고 @agstudy

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

enter image description here

+1

감사 :이 형식을 사용합니다. 'scale_x_date (labels = date_format ("% m- % Y")) +'형식을 사용하겠다. 올해도 x 축에 놓기를 원합니다. – John

+0

향후 독자를 위해 : ggplot2 v2.0.0을 사용하면 더 이상 작동하지 않습니다. 이 게시물 : https://stackoverflow.com/questions/10576095/formatting-dates-with-scale-x-date-in-ggplot2는 올바른 방향으로 이동합니다. – SprengMeister