<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/create-chart-templates-using-r-functions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/functions-chart-templates/?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>R functions can be used to create chart templates, which keep the look and feel of reports consistent. This post gives step by step guide on how to create chart templates using R functions.</p>
<p>R users should already be familiar with calling functions to run calculations and draw plots. However, if you are using a function to create a plot, you need to have a way to remember which fonts and colors to use each time. R functions can be used as a short-cut to creating standard-looking charts, by storing preference for options like font and color.</p>
<p>If you have never created a function in R, or, never created functions to automate the creation of charts, then this post is for you! This post is written for beginners, so if you understand these things, then skip the post.</p>
<hr>
<h2><span>The benefit of using chart templates over modifying R code</span></h2>
<p>Chart templates are useful for quickly creating standard looking charts with your preferred settings (e.g., font sizes, colors, etc.). When you start using R, you do this by saving the code you use to create a chart and then re-using it with small modifications. While this can save some time, it tends to lead to inconsistencies, as when you modify one chart, you forget to modify the code everywhere that you use the same chart. Writing an R function can save you having to reuse the entire code.</p>
<hr>
<h2><span>How to create chart templates using R functions</span></h2>
<p>In this post, I am going to explain how to create a template to reproduce the chart shown below. This one was created with the <span>Displayr/flipStandardCharts</span> package on GitHub, but the principles that I am going to explain can be applied to pretty-much any charting package in R.</p>
<p><iframe src="https://embedded.azureedge.net/75668/232441/ef43ba92-f1cb-4d2b-a0a7-22d1e1758fd0.html?v=28776-fcdfeecfd9-1579322328" width="274" height="277.8269"></iframe></p>
<p>The code used to install the package is:</p>
<pre>
library(devtools)
install_github(&quot;Displayr/flipStandardCharts&quot;)
</pre>
<p>The data is:</p>
<pre>
gdp = c(156080,	89633,	8583,	15275,	12115,	3007,	24204,	1617,	1756,	9601,	617,	7834,	34999,	1727,	336297,	10179,	72374,	14214,	965,	42690,	6299,	1126,	36165,	70529,	2200,	2101,	9991,	5442,	14045,	4635,	12164,	101445,	11015,	10267,	7509,	405083,	8376,	351,	14765,	1427,	3669,	6217,	294841,	9015,	95584,	3727,	47431,	4400,	42063,	25528,	19551,	16289,	19469,	31859,	221415,	2237,	11400,	67430,	20017,	11199145,	1442,	2263523,	932259,	393436,	171489,	318744,	4939384,	38655,	133657,	1411246,	114041,	6551,	15903,	47537,	296359,	3591,	11160,	21144,	66293,	283660,	304905,	152469,	1283162,	646438,	296966,	81322,	6952,	406840,	857749,	36180,	348743,	67220,	202616,	27318,	11927,	10547,	386428,	37848,	47433,	466366,	16560,	52395,	50425,	19802,	192925,	306143,	23137,	236785,	2465454,	14333,	3466757,	194559,	124343,	20047,	294054,	1849970,	27677,	6664,	42739,	59948,	10900,	10949,	6750,	4173,	770845,	370557,	469509,	204565,	186691,	37745,	89552,	43991,	1232088,	511000,	659827,	93270,	2618886,	1449,	9047,	4588,	1765,	1529760,	57436,	87133,	525,	71584,	26797,	1016,	68763,	8023,	21517,	14027,	1045998,	13231,	55188,	917,	1379,	771,	20989,	18569100,	1204616,	4632,	166,	183,	322,	102,	185017,	293,	16929,	786,	1202,	395,	34,	774,	545866,	33806,	1796187,	247028,	282463,	97802,	3446,	27441,	192094,	3621,	52420)
names(gdp) = c('Algeria',	'Angola',	'Benin',	'Botswana',	'Burkina Faso',	'Burundi',	'Cameroon',	'Cape Verde',	'Central African Republic',	'Chad',	'Comoros',	'Congo',	'Democratic Republic of the Congo',	'Djibouti',	'Egypt',	'Equatorial Guinea',	'Ethiopia',	'Gabon',	'Gambia',	'Ghana',	'Guinea',	'Guinea-Bissau',	&quot;Cote d'Ivoire&quot;,	'Kenya',	'Lesotho',	'Liberia',	'Madagascar',	'Malawi',	'Mali',	'Mauritania',	'Mauritius',	'Morocco',	'Mozambique',	'Namibia',	'Niger',	'Nigeria',	'Rwanda',	'Sao Tome and Principe',	'Senegal',	'Seychelles',	'Sierra Leone',	'Somalia',	'South Africa',	'South Sudan',	'Sudan',	'Swaziland',	'Tanzania',	'Togo',	'Tunisia',	'Uganda',	'Zambia',	'Zimbabwe',	'Afghanistan',	'Bahrain',	'Bangladesh',	'Bhutan',	'Brunei Darussalam',	'Myanmar',	'Cambodia',	'China',	'Timor-Leste',	'India',	'Indonesia',	'Iran',	'Iraq',	'Israel',	'Japan',	'Jordan',	'Kazakhstan',	'South Korea',	'Kuwait',	'Kyrgyzstan',	'Lao',	'Lebanon',	'Malaysia',	'Maldives',	'Mongolia',	'Nepal',	'Oman',	'Pakistan',	'Philippines',	'Qatar',	'Russian Federation',	'Saudi Arabia',	'Singapore',	'Sri Lanka',	'Tajikistan',	'Thailand',	'Turkey',	'Turkmenistan',	'United Arab Emirates',	'Uzbekistan',	'Vietnam',	'Yemen',	'Albania',	'Armenia',	'Austria',	'Azerbaijan',	'Belarus',	'Belgium',	'Bosnia and Herzegovina',	'Bulgaria',	'Croatia',	'Cyprus',	'Czech Republic',	'Denmark',	'Estonia',	'Finland',	'France',	'Georgia',	'Germany',	'Greece',	'Hungary',	'Iceland',	'Ireland',	'Italy',	'Latvia',	'Liechtenstein',	'Lithuania',	'Luxembourg',	'Macedonia',	'Malta',	'Moldova',	'Montenegro',	'Netherlands',	'Norway',	'Poland',	'Portugal',	'Romania',	'Serbia',	'Slovakia',	'Slovenia',	'Spain',	'Sweden',	'Switzerland',	'Ukraine',	'United Kingdom',	'Antigua and Barbuda',	'Bahamas',	'Barbados',	'Belize',	'Canada',	'Costa Rica',	'Cuba',	'Dominica',	'Dominican Republic',	'El Salvador',	'Grenada',	'Guatemala',	'Haiti',	'Honduras',	'Jamaica',	'Mexico',	'Nicaragua',	'Panama',	'St. Kitts and Nevis',	'St. Lucia',	'St. Vincent and the Grenadines',	'Trinidad and Tobago',	'United States',	'Australia',	'Fiji',	'Kiribati',	'Marshall Islands',	'Micronesia',	'Nauru',	'New Zealand',	'Palau',	'Papua New Guinea',	'Samoa',	'Solomon Islands',	'Tonga',	'Tuvalu',	'Vanuatu',	'Argentina',	'Bolivia',	'Brazil',	'Chile',	'Colombia',	'Ecuador',	'Guyana',	'Paraguay',	'Peru',	'Suriname',	'Uruguay')
gdp = sort(gdp, decreasing = TRUE) / 1000
</pre>
<p>The chart above was created using:</p>
<pre> 
library(flipStandardCharts)
Chart(gdp[1:10], 
    type = &quot;Bar&quot;,
    data.label.show = TRUE,
    data.label.font.size = 8,
    data.label.font.family = &quot;Arial Narrow&quot;,
    data.label.prefix = &quot;$&quot;,
    data.label.decimals = 0, 
    y.grid.width = 0,
    x.tick.show = FALSE, 
    x.grid.width = 0, 
    x.bounds.minimum = 0,
    x.bounds.maximum = 20000)
</pre>
<hr>
<h2><span>Step 1: Create a simple function</span></h2>
<p>The code below creates a function that produces a chart identical to the one above. Key points to note are that:</p>
<ul>
<li>The first line says we are creating a function called <span>MyBarChartTemplate</span>.</li>
<li>The <span>x</span> on the first line tells us that when we use this function, whatever data we use will be used in place of <span>x</span> when the code below it is run. In this case, it means that the data will be plotted using the <span>Chart</span> function from <span>flipStandardCharts</span>.</li>
<li><span>require(flipStandardCharts)</span> means that we will load the flipStandardCharts package whenever we run the function (if it is not already loaded).</li>
<li>The very last line says to use the function to create a chart of the first 10 numbers in <span>gdp</span>.</li>
<li>Everything else is identical to the example above.</li>
</ul>
<pre> 
MyBarChartTemplate = function(x)
{
    require(flipStandardCharts)
    Chart(x, 
        data.label.show = TRUE,
        data.label.font.size = 8,
        data.label.font.family = &quot;Arial Narrow&quot;,
        data.label.decimals = 0, 
        type = &quot;Bar&quot;,
        y.grid.width = 0,
        x.tick.show = FALSE, 
        x.grid.width = 0, 
        x.bounds.minimum = 0,
        x.bounds.maximum = 20000)
}
MyBarChartTemplate(gdp[1:10])
</pre>
<hr>
<h2><span>Step 2: Apply the function to different data</span></h2>
<p>Now reuse the function. The chart below has been created with a single line of code, with identical formatting (including the scale of the bars, which was my chief motivation in creating the function in this example).</p>
<pre>
MyBarChartTemplate(gdp[11:20])
</pre>
<p><iframe src="https://embedded.azureedge.net/75668/232441/bdb1dd6d-696d-434b-b2d4-e31eef67d8ec.html?v=28776-fcdfeecfd9-369054882" width="274" height="277.8269"></iframe>
</p>
<hr>
<h2><span>Step 3: Add more parameters to the function</span></h2>
<p>The function we have just created only contains a single input, <span>x</span>. It is often useful to create functions that have multiple inputs. The formal name for an input in a function is a <em>parameter. </em>In this example below, I have added a parameter called <span>decimals</span> and given it a default value of 0. Note how I have both added <span>decimals = 0</span> to the first line, and also modified line 9 so that the value of <span>decimals</span> is <em>passed </em>to <span>data.label.decimals</span>. The implications of this are that:</p>
<ol>
<li>If I type <span>MyBarChartTemplate2(gdp[11:20])</span>, I will get exactly the same chart as when using <span>MyBarChartTemplate(gdp[11:20])</span>.</li>
<li>If I instead type <span>MyBarChartTemplate2(gdp[11:20], decimals = 2)</span>, I will get a chart with two decimal points in each of the value labels.</li>
</ol>
<pre>
MyBarChartTemplate2 = function(x, decimals = 0)
{
    require(flipStandardCharts)
    Chart(x, 
        data.label.show = TRUE,
        data.label.font.size = 8,
        data.label.font.family = &quot;Arial Narrow&quot;,
        data.label.prefix = &quot;$&quot;,
        data.label.decimals = decimals,
        type = &quot;Bar&quot;,
        y.grid.width = 0,
        x.tick.show = FALSE, 
        x.grid.width = 0, 
        x.bounds.minimum = 0,
        x.bounds.maximum = 20000)
}
</pre>
<hr>
<h2><span>Step 4: Store and automate</span></h2>
<p>Once you have created functions you find useful, you should create a file or files where you store all of them and paste them into your R sessions as soon as you start working on anything. The next step, after this, is to automate this process by creating your own package. For example, <span>flipStandardCharts</span>, which I used to create the charts in this post is itself a package that my colleagues and I created to make it easier for us to quickly create charts using <span>plotly</span>.</p>
<hr>
<h2><span>Do it yourself</span></h2>
<p>I’ve created each of the examples here inside of Displayr. An advantage of doing this is that when you modify the function, all the charts using the function update automatically without you needing to rerun the code. This is because Displayr works out all the dependencies between calculations and keeps them up-to-date.</p>
<p>You can <a rel="nofollow" href="https://app.displayr.com/Try/Functions%20Chart%20Templates" target="_blank">create your own chart templates using R here</a> – just sign into Displayr to access the document used to create each of the visualizations used in this post. To see the R code, click on a visualization (the R code is shown in <strong>Properties ></strong> <span>R CODE</span> on the right of the screen).</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/functions-chart-templates/?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/Ehk9hxNR9rE" height="1"></a></div>