{"id":2000,"date":"2017-07-14T18:17:04","date_gmt":"2017-07-14T17:17:04","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=2000"},"modified":"2025-07-01T21:02:02","modified_gmt":"2025-07-01T20:02:02","slug":"interactive-plot","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/interactive-plot\/","title":{"rendered":"Interactive Plot"},"content":{"rendered":"\n<p>Rather than creating static plots that can be printed on paper, it is also possible to create interactive plots for publication on a web site. The user can than interact with the plot for example to zoom in or change parameters.<\/p>\n\n\n\n<p>For example, the <a href=\"https:\/\/pcool.dyndns.org\/index.php\/time-series-plots\/\" data-type=\"page\" data-id=\"1667\">time series plot<\/a>&nbsp;could be saved to a web page as an interactive plot, using the&nbsp;use the plotly package<sup class='sup-ref-note' id='note-zotero-ref-p2000-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r1'>1<\/a><\/sup>.<\/p>\n\n\n\n<p>The easiest way to create an interactive plot is to first create the plot using ggplot2<sup class='sup-ref-note' id='note-zotero-ref-p2000-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r2'>2<\/a><\/sup> and then convert it to an interactive plot with the plotly<sup class='sup-ref-note' id='note-zotero-ref-p2000-r3-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r3'>3<\/a><\/sup>.<\/p>\n\n\n\n<p><strong>1: Create the plot&nbsp;<\/strong><\/p>\n\n\n\n<p>The data set&nbsp;<a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/temp.rda\" target=\"_blank\" rel=\"noreferrer noopener\">temp<\/a>&nbsp;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><span style=\"color: #0000ff;\"><span style=\"color: #ff0000;\">&gt; temp<\/span>\ntemp dewpoint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Date\n1&nbsp;&nbsp; 11.9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7.1 2015-05-11\n2&nbsp;&nbsp; 14.2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.7 2015-05-12\n3&nbsp;&nbsp; 14.2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.7 2015-05-12<\/span><\/em>\n<em><span style=\"color: #0000ff;\"><span style=\"color: #000000;\">--- omitted for brevity<\/span><\/span><\/em><\/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-p2000-r4-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r4'>4<\/a><\/sup> and scales<sup class='sup-ref-note' id='note-zotero-ref-p2000-r5-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r5'>5<\/a><\/sup> packages are <a href=\"https:\/\/pcool.dyndns.org\/index.php\/packages\/\" data-type=\"page\" data-id=\"22\">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 library, 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\">Please note that the code can be copied and pasted into the 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\n\n\n<p><strong>2: Convert to an interactive plot<\/strong><\/p>\n\n\n\n<p>The above plot can now be converted to an interactive plot with the plotly package<sup class='sup-ref-note' id='note-zotero-ref-p2000-r6-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r6'>6<\/a><\/sup>.<\/p>\n\n\n\n<p>Define the plot as an object and give it a name (ie temp_plot).<\/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:#f60202\" class=\"has-inline-color\">temp_plot &lt;- <\/mark><\/em><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--2\">Please note that the code can be copied and pasted into the console.<\/p>\n\n\n\n<p>The plot will now show but will be stored as an object. To show the plot in R, simply enter:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">temp_plot<\/span><\/em><\/code><\/pre>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--3\">plot already shown above<\/p>\n\n\n\n<p>The plotly package<sup class='sup-ref-note' id='note-zotero-ref-p2000-r7-o1'><a class='sup-ref-note' href='#zotero-ref-p2000-r7'>7<\/a><\/sup> should be <a href=\"https:\/\/pcool.dyndns.org\/index.php\/packages\/\" data-type=\"page\" data-id=\"22\">installed<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">library(plotly)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggplotly(temp_plot)<\/span><\/em><\/code><\/pre>\n\n\n\n<p>The plot should now appear in your web browser. The folder that contains the files to create the plot is shown in the address bar of your browser. This folder will be deleted when you close R, so leave R open. Go to the folder shown in the address bar of your browser and copy the folder with all its subfolders and paste it to your web server. Set up a link and the plot should be available on your server:<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/weblinks\/tempplot\" target=\"_blank\" rel=\"noreferrer noopener\">Temperature plot<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rather than creating static plots that can be printed on paper, it is also possible to create interactive plots for publication on a web site. The user can than interact with the plot for example to zoom in or change parameters. For example, the time series plot&nbsp;could be saved to a web page as an [&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-2000","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2000","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=2000"}],"version-history":[{"count":5,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2000\/revisions"}],"predecessor-version":[{"id":4746,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2000\/revisions\/4746"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=2000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}