{"id":1667,"date":"2016-05-02T13:12:22","date_gmt":"2016-05-02T12:12:22","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=1667"},"modified":"2025-07-01T20:49:16","modified_gmt":"2025-07-01T19:49:16","slug":"time-series-plots","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/time-series-plots\/","title":{"rendered":"Time Series"},"content":{"rendered":"\n<p>Sometimes it is useful to display a variable over time. This example shows how to create a time series using the ggplot2<sup class='sup-ref-note' id='note-zotero-ref-p1667-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p1667-r1'>1<\/a><\/sup> library.<\/p>\n\n\n\n<p>The data set <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/temp.rda\" target=\"_blank\" rel=\"noreferrer noopener\">temp<\/a> contains the daily temperature and dew point. It contains three variables: Date, temp and dewpoint. Once loaded into R, the data frame can be viewed:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f70a2a\" class=\"has-inline-color\">temp<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0a14f8\" class=\"has-inline-color\">\n    temp dewpoint       Date\n1   11.9      7.1 2015-05-11\n2   14.2      4.7 2015-05-12\n3   14.2      4.7 2015-05-12\n4   14.2      4.7 2015-05-12<\/mark><\/em>\n.....\nomitted for brevity<\/code><\/pre>\n\n\n\n<p>The individual variables can be addressed:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>temp$Date<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>temp$temp<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>temp$dewpoint<\/em><\/span><\/code><\/pre>\n\n\n\n<p>To create a time series of the temperature and the dew point, first make sure the ggplot2<sup class='sup-ref-note' id='note-zotero-ref-p1667-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p1667-r2'>2<\/a><\/sup> and scales<sup class='sup-ref-note' id='note-zotero-ref-p1667-r3-o1'><a class='sup-ref-note' href='#zotero-ref-p1667-r3'>3<\/a><\/sup> packages are <a href=\"https:\/\/pcool.dyndns.org\/index.php\/packages\/\" data-type=\"page\" data-id=\"22\">installed and loaded<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>library(scales) # to access breaks\/formatting functions<\/em><\/span><\/code><\/pre>\n\n\n\n<p>Now create a new window (dev.new()), use the ggplot package, apply a black and white theme, show data points and fit a smooth trend line (red for temp and blue for dewpoint). Following this, create a title and appropriate axes labels. When adding annotations, it is necessary to address the coordinates of the x-axis as a date (rather than a number or string). Finally, format the date axis as appropriate (here 3 monthly):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>dev.new()<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggplot() +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw() +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point(aes(x=Date, y=temp), data=temp, colour='#ff0000') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_smooth(aes(x=Date, y=temp), data=temp, colour='#ff0000', method='loess') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point(aes(x=Date, y=dewpoint), data=temp, shape=17, <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>colour='#0000ff') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_smooth(aes(x=Date, y=dewpoint), data=temp, method='loess') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(label='Temperature and Dew Point') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ylab(label='Temp deg Celsius') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>xlab(label='Date') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>annotate(geom='text', x=as.Date('2015-06-15'), y=28,<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>label='Temperature', fontface='bold') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>annotate(geom='text', x=as.Date('2015-06-15'), y=26, label='Dew Point', fontface='bold') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>annotate('segment', x=as.Date('2015-05-01'), xend=as.Date('2015-05-10'), y=28, yend=28, colour='red') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>annotate('segment', x=as.Date('2015-05-01'), xend=as.Date('2015-05-10'), y=26, yend=26, colour='blue') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_x_date(labels=date_format('%m\/%y'), <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>breaks=date_breaks('3 months')) # this scales the x axis.<\/em><\/span><\/code><\/pre>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">The code can be copied and pasted into the R console<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"919\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-1024x919.png\" alt=\"\" class=\"wp-image-3812\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-1024x919.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-300x269.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-768x690.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-1536x1379.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/temp-2048x1839.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is useful to display a variable over time. This example shows how to create a time series using the ggplot2 library. The data set temp contains the daily temperature and dew point. It contains three variables: Date, temp and dewpoint. Once loaded into R, the data frame can be viewed: The individual variables [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-1667","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=1667"}],"version-history":[{"count":4,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1667\/revisions"}],"predecessor-version":[{"id":4741,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1667\/revisions\/4741"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=1667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}