We might often find ourselves using similar code repetitively to plot similar kinds of data or different versions of the same dataset. Once we have analyzed our data and are looking to produce a finished graph, it can be useful to quickly try out different color combinations and other aesthetic settings without having to write too much repetitive code. In this recipe, we will learn how to create graph templates and use them to quickly try out various looks for a graph.
We will only use the base graphics functions for this recipe. So, just open up the R prompt and type in the following code. We will use the themes.csv
file that contains theme parameters for this recipe. So, let's first load it:
themes<-read.csv("themes.csv")
We will make a simple scatter plot showing a random normal distribution and apply different color combination themes to it with a single command:
themeplot<-function(x,theme,...) { i<-which(themes$theme==theme) par(bg=as.character(themes[i,]$bg_color),las=1) plot(x,type="n",...) u<-par("usr") plotcol=as.character(themes[i,]$plot_color) rect(u[1],u[3],u[2],u[4],col=plotcol,border=plotcol) points(x,col=as.character(themes[i,]$symbol_color),...) box() }
Using this function, we can create a scatter plot using different themes such as the following:
themeplot(rnorm(1000),theme="white",pch=21,main="White")
themeplot(rnorm(1000),theme="lightgray",pch=21,main="Light Gray")
themeplot(rnorm(1000),theme="dark",pch=21,main="Dark")
themeplot(rnorm(1000),theme="pink",pch=21,main="Pink")
In the preceding example, we created a function called themeplot()
that used predefined color combinations from the themes.csv
file to create different themed graphs.
We first read the themes.csv
file into a data frame called themes that contains four columns:
theme
: This is the name of the themebg_color
: This is the figure background colorplot_color
: This is the color of the plot regionsymbol_color
: This is the color of the plotting symbolWe then created the themeplot()
function that accepted the plotting variable x
and theme
as arguments. The trailing "..."
means that additional arguments can be passed; these are passed on to the specified functions within the themeplot()
function definition. The themeplot()
function uses the which()
function to find the row index of the specified theme and then uses the corresponding column values to set the figure background color in par()
, the plot region color in rect()
, and the symbol color in points()
.
Once the function is defined, all we have to do to try out different color combinations is pass the theme
argument to themeplot()
. If we wish to modify the color combinations or add new themes, we can simply edit the themes.csv
file and re-read it. We can also adjust the function definition so that we can pass the color values separately to override the theme specifications.
In this example, we chose some very simple color parameters to demonstrate the usefulness of themes. However, we can easily add more columns to the themes definitions, such as symbol types, sizes, line types and colors, fonts, grid line styles, legend styles, and so on. It is best to work with your own dataset, define themes as you go along, and have a better idea of what your specific requirements are. Once you have decided on the structure of the graph you can define various themes to quickly experiment and choose from.
3.16.48.122