<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-add-trend-lines-to-visualizations-in-displayr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/how-to-add-trend-lines-to-visualizations-in-displayr/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In Displayr, Visualizations of chart type Column, Bar, Area, Line and Scatter all support trend lines.  Trend lines can be  linear or non-parametric (cubic spline, Friedman’s super-smoother or LOESS).</p>
<h2>Adding a linear trend line</h2>
<p>Linear trend lines can be added to a chart by fitting a regression to each series in the data source. In the chart below, the linear trends are shown as dotted lines in the color corresponding to the data series. We see there is considerable fluctuation in the frequency of each search term. But the trend lines clarify that the overall trend for SPSS is downward, whereas the trend for Stata is increasing.</p>
<p><img sizes="(max-width: 857px) 100vw, 857px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/trendline-in-displayr-linear.png?w=450&ssl=1" alt="Linear trend lines" class="wp-image-5719" data-recalc-dims="1"></p>
<p>The data for this chart was generated by clicking <strong>Insert</strong> > <strong>More</strong> > <strong>Data</strong> > <strong>Google Trends</strong>. In the textbox for <em>Topic(s)</em> we typed in a comma-separated list of search terms (i.e., “SPSS, Stata”). This creates a table scoring the number of times each term was searched for each week. Any input table that has a similar structure to this can be used to create a chart. <img sizes="(max-width: 375px) 100vw, 375px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/07/trendline-in-displayr-data.png?resize=375%2C258&ssl=1" alt="Time series data" width="375" class="wp-image-5720" data-recalc-dims="1" height="258"></p>
<p>If we click on the table, the object inspector showing the properties of this output is shown on the right. Under the <em>Properties</em> tab, expand the <em>General</em> group to see the name of the table, in this case <span>google.trends</span>.</p>
<p>We create a chart by selecting <strong>Insert</strong> > <strong>Visualizations</strong> > <strong>Line</strong>. In the dropdown for <em>Output in ‘Pages’</em> select the name of the input table (i.e., <span>google.trends</span>). On the <em>Chart</em> tab in the object inspector, look for the <em>Trend lines</em> group. Set the <em>Line of best fit</em> dropdown to <strong>Linear</strong>. We also tick the checkbox for <em>Ignore last data point.  </em>This option is useful for ignoring the last time period which may be incomplete if the data is in the process of being collected.</p>
<h2>Trend lines using non-parametric smoothers</h2>
<p>In many cases, we want to estimate a trend that is not constrained to a straight line. To estimate smooth (non-parametric) trend lines, we can use cubic splines, Friedman’s super smoother or LOESS. Note that LOESS uses a fixed span of 0.75 which may sometimes be overly large. In contrast, the cubic spline and Friedman’s smoother uses cross-validation to select a span, and they are usually better at identifying the important features. For example, in the figure below, the LOESS trend line suggests there is a gradual decrease in river flow from 1870 to 1900. However, the cubic spline and Friedman’s super smoother picks up a sharp decrease in 1989.</p>
<p>This example uses Nile, which is a built-in dataset in R. It is a time-series object, so to load the dataset in Displayr, first create an R Output and type the following code in the textbox for R code:</p>
<pre>
data(Nile)
x = Nile
</pre>
<p>The second line is necessary to assign a name to the time series data. You will then be able to use the data set in a chart by selecting <span>x</span> in the <em>Output in ‘Pages’</em> dropdown (under <em>Data Source</em> in the <em>Inputs</em> tab).</p>
<p><img sizes="(max-width: 595px) 100vw, 595px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/07/trendline-in-q-cubic.jpg?w=450&ssl=1" alt="Cubic spline trend line" class="wp-image-5709" data-recalc-dims="1"></p>
<p><img sizes="(max-width: 590px) 100vw, 590px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/trendline-in-q-supsmu.jpg?w=450&ssl=1" alt="super smoother trend line" class="wp-image-5710" data-recalc-dims="1"><img sizes="(max-width: 590px) 100vw, 590px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/07/trendline-in-q-loess-1.jpg?w=450&ssl=1" alt="LOESS trend line" class="wp-image-5715" data-recalc-dims="1"></p>
<p>Trend lines are added by going to the <em>Chart</em> tab in the object inspector and selecting a method (cubic spline, Friedman’s super smoother or LOESS) for the line of best fit. Once this option is selected, other controls to customize the appearance of line of best fit will be shown under the <em>Trend Lines</em> group. You may also want to adjust the opacity or the color palette (under the <em>Data series</em> group) of the data (i.e., column bars).</p>
<p><strong>To find out more about <a rel="nofollow" href="https://www.displayr.com/blog/" target="_blank">visualizations in Displayr</a>, head to our blog now. Ready to use trend lines for your own data? Grab your <a rel="nofollow" href="https://app.displayr.com/SignUp?plan_id=0" target="_blank">free trial of Displayr</a> now.</strong></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.displayr.com/how-to-add-trend-lines-to-visualizations-in-displayr/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/LU6e-wotpic" height="1"></a></div>