Plotting time series with ggplot2

3,706 views
Skip to first unread message

jpm miao

unread,
Jun 11, 2013, 5:06:58 AM6/11/13
to ggp...@googlegroups.com
Hi, 

  I try to plot a time series data as follow but an error message emerges.

date1<-seq(as.yearmon("1998-1"), as.yearmon("2013-2"), by=1/12)
y_dl["date1"]<-date1
ggplot(y_dl, aes(x=date1, y=dlfx))+geom_line()+
  scale_x_date(labels = date_format("%Y"))

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

How could I input the dates so that the time series graph can be plotted? Thanks, 

Miao 



> dput(y_dl)
structure(list(dlfx = c(0.045722786472806, -0.0315742577875144, 
-0.00987193342995906, 0.0152882812665033, 0.0147432317974693, 
0.0289133759879036, -0.00245199228912973, 0.0093849719305723, 
-0.0034399687367852, -0.045768138360914, -0.0153767735858099, 
-0.00697191411709452, -0.00138309088175114, 0.0095010646842768, 
0.0169887869904838, -0.00666468965077582, -0.00416886650199765, 
-0.00839138996426669, -0.00647977114194908, -0.00850090351561183, 
-0.00624097095080999, -0.00105808370352589, -0.00101067026032941, 
-0.00389626190591796, -0.0259684745642396, -0.00257165903371081, 
-0.000752061180021357, -0.0068207445801347, 0.00813154646839731, 
0.00224868908026287, 0.00433502256862583, 0.00444251916252147, 
0.00362973991261617, 0.0201862718266463, 0.0185170612258405, 
0.0201521577670278, -0.0120204597125352, -0.0113595237065302, 
0.00824548413378467, 0.0101180550823474, 0.0100443398593772, 
0.0302275494720101, 0.014709997677254, -0.00472158755699148, 
-0.000825685215709359, -0.00110308282064153, -0.00151239468250485, 
0.00573932764805951, 0.00938553510005357, 0.00124886991900119, 
-0.00136602432027599, -0.00300355323072887, -0.0130920057504906, 
-0.0137590563257434, -0.0173197272160736, 0.0170570904544056, 
0.0177908096680714, 0.0104103932201287, -0.00690788097636563, 
0.00396536848971962, -0.00694787595196544, 0.00518646325449578, 
-0.00185319991207899, 0.00291761538302504, -0.00310323351731867, 
-0.00269707822157228, -0.00599535570148734, -0.00234124285828718, 
-0.00993088430263, -0.00293495242260632, 0.0045306438402104, 
0.000231669796523448, -0.0109645644623928, -0.0137366665544127, 
0.00237696953660737, -0.00906331967062445, 0.0149940864378672, 
0.00414396454164078, 0.00734316792319145, 0.00669574892973168, 
-0.00583566421517556, -0.00333255471619153, -0.0310475866785386, 
-0.016853704366551, -0.00897265937825064, -0.0133129942197718, 
-0.0112477640248664, 0.0114600073666402, -0.00698469955569924, 
0.00215642615248956, 0.0173924957133935, 0.00592709051437934, 
0.0249917412054458, 0.0170952345728365, 0.00265134376764786, 
-0.00841694724873054, -0.0364548843812579, 0.00820722109948279, 
0.00361014829247086, -0.00547726438824591, -0.0171427521487932, 
0.0223455186147503, 0.00467188754898417, 0.00482435984192219, 
0.00356985164449241, 0.00905999121954881, -0.0115927269410316, 
-0.00918453101412675, 0.00749433619871764, 0.00609982958055477, 
0.0013234848673127, 0.00402188811475801, 0.00346892406649645, 
-0.00992052464348125, -0.00434317302909592, 0.00496081164866568, 
0.000959440781235443, -0.0131909703541119, -0.00676469164951454, 
0.00262612981681531, -0.00152579900868055, -0.0235711965058844, 
-0.0324611260274134, -0.00835783852346105, 0.00827310617050525, 
-0.0077320283396598, 0.00134485608009482, 0.0254783777984726, 
0.0242347988108822, 0.0226548544959062, 0.0129669130023075, 0.000917619005314396, 
0.00554126689345624, 0.0280197721098374, 0.00182316679489513, 
-0.0189478945992323, -0.0236649168139151, -0.00351809942574199, 
0.00390340006205436, -0.00110556485854873, -0.00903195998684758, 
-0.0079437088538663, 0.000205476708416529, -0.00177482455068611, 
-0.0119415363088339, 0.00617970634469156, -0.00678500719938668, 
-0.011274017909169, 0.0139323733379521, 0.0107329329417829, -0.0041574619460083, 
-0.00699124827243879, -0.00536128756783638, -0.0260755525220401, 
-0.00822780318028515, -0.0051172377521107, -0.0311418989772289, 
-0.00739230993254524, 0.00456026091154049, -0.017751143468685, 
-0.00673982363143554, 0.00202481005176435, -0.000487643790346936, 
0.00479972453257859, 0.0254329308879266, 0.017806637509611, -0.00122535652023758, 
0.000996317979801375, -0.00699262110030218, -0.0167158161499663, 
-0.000564526880268978, -0.0014101942665996, 0.000616275795373155, 
0.0142920948144551, 0.00199912213931563, -0.000649267504066575, 
-0.0127508123599828, -0.00925730170667771, -0.00513400212897119, 
-0.00226399657626386, 0.00222973181243046, 0.0163472858186852
), flow = c(134.66, 1162.06, -764.05, -380.91, 150.22, -456.23, 
361.38, -43.76, -185.02, 553.22, 250.96, -190.9, 475.2, -110.42, 
794.43, 1158.16, -449.15, 62.4400000000001, -49.48, 551.06, 140, 
-852, 1077, 489, 1385, 1027, 214, -3162, 698, 1221, -922, 784, 
-879, 476, 1334, -1479, 1826, -845, -152, -307, 1470, -743, 10, 
-280, -727, 1459, 638, -1156, 903, -1496, 1843, 72, -1339, 480, 
-1236, -215, -304, 818, 1197, -735, 676, -703, 709, -136, 1631, 
742, 1010, 1644, -761, 53547, 425, 837, 4075, 2231, 2848, 282, 
-3210, -746, -495, 698, 742, 2396, 4103, 831, 1577, 3741, 1352, 
878, 3374, 4809, 1399, -866, -635, -619, 5313, 8527, 3488, 1873, 
129, 7097, -57, -1829, -907, 1375, 1697, 1634, 4286, 2597, 1657, 
1699, -2316, 2877, 1607, 6441, 2028, -7210, 702, 1937, -1555, 
-602, 2299, 7250, 6417, -211, -2082, -1233, -5806, -4972, -5234, 
-7522, -2489, 746, -1901, -617, 1449, 2870, 6962, 854, 2478, 
-67, 7047, 2925, 2000, 2578, 2886, -1899, 3578, 4630, -2312, 
695, -837, -2651, 2351.23000000001, 2191.76999999999, 1400, 4382, 
3571, -2168, -795, 4253, 458, 465, -2269, -8022, -4104, 591, 
-1942, 169, 3021, 937, 2918, -291, -3295, -345, -2793, 1839, 
406, 790, 1262, 2919, 661, 1836), date1 = structure(c(1998, 1998.08333333333, 
1998.16666666667, 1998.25, 1998.33333333333, 1998.41666666667, 
1998.5, 1998.58333333333, 1998.66666666667, 1998.75, 1998.83333333333, 
1998.91666666667, 1999, 1999.08333333333, 1999.16666666667, 1999.25, 
1999.33333333333, 1999.41666666667, 1999.5, 1999.58333333333, 
1999.66666666667, 1999.75, 1999.83333333333, 1999.91666666667, 
2000, 2000.08333333333, 2000.16666666667, 2000.25, 2000.33333333333, 
2000.41666666667, 2000.5, 2000.58333333333, 2000.66666666667, 
2000.75, 2000.83333333333, 2000.91666666667, 2001, 2001.08333333333, 
2001.16666666667, 2001.25, 2001.33333333333, 2001.41666666667, 
2001.5, 2001.58333333333, 2001.66666666667, 2001.75, 2001.83333333333, 
2001.91666666667, 2002, 2002.08333333333, 2002.16666666667, 2002.25, 
2002.33333333333, 2002.41666666667, 2002.5, 2002.58333333333, 
2002.66666666667, 2002.75, 2002.83333333333, 2002.91666666667, 
2003, 2003.08333333333, 2003.16666666667, 2003.25, 2003.33333333333, 
2003.41666666667, 2003.5, 2003.58333333333, 2003.66666666667, 
2003.75, 2003.83333333333, 2003.91666666667, 2004, 2004.08333333333, 
2004.16666666667, 2004.25, 2004.33333333333, 2004.41666666667, 
2004.5, 2004.58333333333, 2004.66666666667, 2004.75, 2004.83333333333, 
2004.91666666667, 2005, 2005.08333333333, 2005.16666666667, 2005.25, 
2005.33333333333, 2005.41666666667, 2005.5, 2005.58333333333, 
2005.66666666667, 2005.75, 2005.83333333333, 2005.91666666667, 
2006, 2006.08333333333, 2006.16666666667, 2006.25, 2006.33333333333, 
2006.41666666667, 2006.5, 2006.58333333333, 2006.66666666667, 
2006.75, 2006.83333333333, 2006.91666666667, 2007, 2007.08333333333, 
2007.16666666667, 2007.25, 2007.33333333333, 2007.41666666667, 
2007.5, 2007.58333333333, 2007.66666666667, 2007.75, 2007.83333333333, 
2007.91666666667, 2008, 2008.08333333333, 2008.16666666667, 2008.25, 
2008.33333333333, 2008.41666666667, 2008.5, 2008.58333333333, 
2008.66666666667, 2008.75, 2008.83333333333, 2008.91666666667, 
2009, 2009.08333333333, 2009.16666666667, 2009.25, 2009.33333333333, 
2009.41666666667, 2009.5, 2009.58333333333, 2009.66666666667, 
2009.75, 2009.83333333333, 2009.91666666667, 2010, 2010.08333333333, 
2010.16666666667, 2010.25, 2010.33333333333, 2010.41666666667, 
2010.5, 2010.58333333333, 2010.66666666667, 2010.75, 2010.83333333333, 
2010.91666666667, 2011, 2011.08333333333, 2011.16666666667, 2011.25, 
2011.33333333333, 2011.41666666667, 2011.5, 2011.58333333333, 
2011.66666666667, 2011.75, 2011.83333333333, 2011.91666666667, 
2012, 2012.08333333333, 2012.16666666667, 2012.25, 2012.33333333333, 
2012.41666666667, 2012.5, 2012.58333333333, 2012.66666666667, 
2012.75, 2012.83333333333, 2012.91666666667, 2013, 2013.08333333333
), class = "yearmon")), row.names = c(NA, -182L), .Names = c("dlfx", 
"flow", "date1"), class = "data.frame")


Ista Zahn

unread,
Jun 11, 2013, 8:37:16 AM6/11/13
to jpm miao, ggp...@googlegroups.com
Just convert it to a Date:

y_dl[["date1"]] <- as.Date(y_dl[["date1"]])

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

jpm miao

unread,
Jun 13, 2013, 3:05:32 AM6/13/13
to Matthias Rieber, ggp...@googlegroups.com
Hi, 

   Thank you very much for the reply. Is there any way to declare the date directly by the function "Date" instead of coercing the date type by "as.Date"? 
  Could it be something like:
> temp<-seq("1998-01", "2013-02", by="1 month")
Error in to - from : non-numeric argument to binary operator

   Thanks, 

Miao 


2013/6/11 Matthias Rieber <matt...@zu-con.org>
Hi,



Am Dienstag, 11. Juni 2013 11:06:58 UTC+2 schrieb Miao:
Hi, 

  I try to plot a time series data as follow but an error message emerges.

date1<-seq(as.yearmon("1998-1"), as.yearmon("2013-2"), by=1/12)
y_dl["date1"]<-date1
ggplot(y_dl, aes(x=date1, y=dlfx))+geom_line()+
  scale_x_date(labels = date_format("%Y"))

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

try:

y_dl$date1 <- as.Date(y_dl$date1)

Matthias

Ben Bolker

unread,
Jun 13, 2013, 8:17:41 AM6/13/13
to ggp...@googlegroups.com
On 13-06-13 03:05 AM, jpm miao wrote:
> Hi,
>
> Thank you very much for the reply. Is there any way to declare the
> date directly by the function "Date" instead of coercing the date type
> by "as.Date"?
> Could it be something like:
>> temp<-seq("1998-01", "2013-02", by="1 month")
> Error in to - from : non-numeric argument to binary operator
>
> Thanks,
>
> Miao

I don't know if

seq.Date(as.Date("1998-01-01"),as.Date("2013-02-01"),by="1 month")

helps.

>
>
> 2013/6/11 Matthias Rieber <matt...@zu-con.org <mailto:matt...@zu-con.org>>
>
> Hi,
>
>
> Am Dienstag, 11. Juni 2013 11:06:58 UTC+2 schrieb Miao:
>
> Hi,
>
> I try to plot a time series data as follow but an error
> message emerges.
>
> date1<-seq(as.yearmon("1998-1"__), as.yearmon("2013-2"), by=1/12)
> y_dl["date1"]<-date1
> ggplot(y_dl, aes(x=date1, y=dlfx))+geom_line()+
> scale_x_date(labels = date_format("%Y"))
>
> Error: Invalid input: date_trans works with objects of class
> Date only
>
>
> try:
>
> y_dl$date1 <- as.Date(y_dl$date1)
>
> Matthias
>
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>.

Daniel Nvule

unread,
Jun 17, 2013, 3:01:49 PM6/17/13
to ggp...@googlegroups.com
Hi,
Convert the y_dl  dataframe with  the year.mon dates to a zoo object via the xts  package and then plot.
Assuming you have all the required packages, the following 3 lines of code should do it.
library(xts)
yy<-xts(y_dl[,-3],as.Date(y_dl[,3]))
autoplot(yy[,2])
 
Daniel
Reply all
Reply to author
Forward
0 new messages