Create Chart Templates Using R Functions
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
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.
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.
The benefit of using chart templates over modifying R code
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.
How to create chart templates using R functions
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 Displayr/flipStandardCharts package on GitHub, but the principles that I am going to explain can be applied to pretty-much any charting package in R.
The code used to install the package is:
library(devtools) install_github("Displayr/flipStandardCharts")
The data is:
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', "Cote d'Ivoire", '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
The chart above was created using:
library(flipStandardCharts) Chart(gdp[1:10], type = "Bar", data.label.show = TRUE, data.label.font.size = 8, data.label.font.family = "Arial Narrow", data.label.prefix = "$", data.label.decimals = 0, y.grid.width = 0, x.tick.show = FALSE, x.grid.width = 0, x.bounds.minimum = 0, x.bounds.maximum = 20000)
Step 1: Create a simple function
The code below creates a function that produces a chart identical to the one above. Key points to note are that:
- The first line says we are creating a function called MyBarChartTemplate.
- The x on the first line tells us that when we use this function, whatever data we use will be used in place of x when the code below it is run. In this case, it means that the data will be plotted using the Chart function from flipStandardCharts.
- require(flipStandardCharts) means that we will load the flipStandardCharts package whenever we run the function (if it is not already loaded).
- The very last line says to use the function to create a chart of the first 10 numbers in gdp.
- Everything else is identical to the example above.
MyBarChartTemplate = function(x) { require(flipStandardCharts) Chart(x, data.label.show = TRUE, data.label.font.size = 8, data.label.font.family = "Arial Narrow", data.label.decimals = 0, type = "Bar", y.grid.width = 0, x.tick.show = FALSE, x.grid.width = 0, x.bounds.minimum = 0, x.bounds.maximum = 20000) } MyBarChartTemplate(gdp[1:10])
Step 2: Apply the function to different data
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).
MyBarChartTemplate(gdp[11:20])
Step 3: Add more parameters to the function
The function we have just created only contains a single input, x. It is often useful to create functions that have multiple inputs. The formal name for an input in a function is a parameter. In this example below, I have added a parameter called decimals and given it a default value of 0. Note how I have both added decimals = 0 to the first line, and also modified line 9 so that the value of decimals is passed to data.label.decimals. The implications of this are that:
- If I type MyBarChartTemplate2(gdp[11:20]), I will get exactly the same chart as when using MyBarChartTemplate(gdp[11:20]).
- If I instead type MyBarChartTemplate2(gdp[11:20], decimals = 2), I will get a chart with two decimal points in each of the value labels.
MyBarChartTemplate2 = function(x, decimals = 0) { require(flipStandardCharts) Chart(x, data.label.show = TRUE, data.label.font.size = 8, data.label.font.family = "Arial Narrow", data.label.prefix = "$", data.label.decimals = decimals, type = "Bar", y.grid.width = 0, x.tick.show = FALSE, x.grid.width = 0, x.bounds.minimum = 0, x.bounds.maximum = 20000) }
Step 4: Store and automate
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, flipStandardCharts, 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 plotly.
Do it yourself
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.
You can create your own chart templates using R here – 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 Properties > R CODE on the right of the screen).
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.