How to plot time series of monthly values in ggplot

98 views
Skip to first unread message

lily li

unread,
Oct 12, 2018, 12:36:45 AM10/12/18
to ggp...@googlegroups.com
Hi ggplot users,

I have a dataset like this exactly.

How to draw time series of x-axis as monthly data, such as 1993-01, 1993-02, ..., 1993-12, 1994-01, ..., 1994-12, etc. Then y-axis as the values in ggplot2? I searched online, and one way seems to compile the dataset as 1993-01-01, 1993-02-01, etc, and then plot it as monthly values. But I see if there is a way to plot the monthly data directly? Thanks for any kind of help.

Shreya Agarwal

unread,
Oct 12, 2018, 2:21:51 AM10/12/18
to choc...@gmail.com, ggp...@googlegroups.com
Hi Lily,

The dataset you're referring to is not tidy (vertical data). Once you have months as columns and all the values under another separate  column, you'll have clearly defined x-values and y-values. Hope that helps. 

(scroll down to 'spread' and 'gather' part on the webpage shared above)

Please let me know if you have any questions.

Thanks,
Shreya

--
--
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/d/optout.


--
Regards,
Shreya Agarwal

"A setback is nothing but a setup for a comeback!"

Fahad Usman

unread,
Oct 13, 2018, 7:17:19 PM10/13/18
to shrey...@gmail.com, choc...@gmail.com, ggp...@googlegroups.com
yes, as Shreya said. tidy it first please. you will need to use the function gather()

lily li

unread,
Oct 15, 2018, 12:39:01 AM10/15/18
to fahad...@gmail.com, shrey...@gmail.com, ggp...@googlegroups.com
Thanks for your reply. 
I'm still not clear how to do it. Should I combine the year and month to year-month format first, such as 1993-01, 1993-02, etc?

lily li

unread,
Oct 15, 2018, 4:00:20 AM10/15/18
to fahad...@gmail.com, shrey...@gmail.com, ggp...@googlegroups.com
I have added one more column 'day' as 1, so can plot it. I did a search and found that ggplot recognizes year-month-day rather than year-month as the x-axis.

Paul Bivand

unread,
Oct 19, 2018, 1:40:07 PM10/19/18
to choc...@gmail.com, fahad...@gmail.com, shrey...@gmail.com, ggp...@googlegroups.com
I normally do this by putting my data through the zoo package as
zooreg(). and then using autoplot.zoo() which gives an x axis in
scale_x_yearmon() where you can specify how the dates are represented.
Zoo formats (and the related xts ones) have the advantage that there
are a lot of time series analysis methods that work for them.

Paul Bivand

Patrick Hubers

unread,
May 3, 2019, 7:00:03 AM5/3/19
to ggplot2
No, you can keep year and month as separate columns and then use

ggplot(data, aes(x = interaction(month, year) y= value)



Op maandag 15 oktober 2018 06:39:01 UTC+2 schreef lily li:
Thanks for your reply. 
I'm still not clear how to do it. Should I combine the year and month to year-month format first, such as 1993-01, 1993-02, etc?

yes, as Shreya said. tidy it first please. you will need to use the function gather()

On Fri, Oct 12, 2018 at 7:21 AM Shreya Agarwal <shrey...@gmail.com> wrote:
Hi Lily,

The dataset you're referring to is not tidy (vertical data). Once you have months as columns and all the values under another separate  column, you'll have clearly defined x-values and y-values. Hope that helps. 

(scroll down to 'spread' and 'gather' part on the webpage shared above)

Please let me know if you have any questions.

Thanks,
Shreya

On Fri, Oct 12, 2018 at 10:06 AM lily li <choc...@gmail.com> wrote:
Hi ggplot users,

I have a dataset like this exactly.

How to draw time series of x-axis as monthly data, such as 1993-01, 1993-02, ..., 1993-12, 1994-01, ..., 1994-12, etc. Then y-axis as the values in ggplot2? I searched online, and one way seems to compile the dataset as 1993-01-01, 1993-02-01, etc, and then plot it as monthly values. But I see if there is a way to plot the monthly data directly? Thanks for any kind of help.

--
--
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 ggp...@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 ggp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Regards,
Shreya Agarwal

"A setback is nothing but a setup for a comeback!"

--
--
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 ggp...@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 ggp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages