#Make a theme
#This code can be modified and run prior to making graphs.
#You can add it to a project's script, or keep it in a separate script
#Then this format template can be applied by adding par.settings = "mytheme" to a graphing command.
#This reduces the number of formatting command that you need to add into graphing commands.
#You can always modify and re-run these commands to tweak a graph's look.
#You can run one of these commands at a time just to change one thing.
#You can also override any parameter in the graphing command if you don't want to change your global settings.
require(lattice)
## Loading required package: lattice
mytheme <- standard.theme("pdf") #this gets us started with a standard theme
names(mytheme) #this gives us a list of all the graph characteristics we can change
## [1] "grid.pars" "fontsize" "background"
## [4] "panel.background" "clip" "add.line"
## [7] "add.text" "plot.polygon" "box.dot"
## [10] "box.rectangle" "box.umbrella" "dot.line"
## [13] "dot.symbol" "plot.line" "plot.symbol"
## [16] "reference.line" "strip.background" "strip.shingle"
## [19] "strip.border" "superpose.line" "superpose.symbol"
## [22] "superpose.polygon" "regions" "shade.colors"
## [25] "axis.line" "axis.text" "axis.components"
## [28] "layout.heights" "layout.widths" "box.3d"
## [31] "par.xlab.text" "par.ylab.text" "par.zlab.text"
## [34] "par.main.text" "par.sub.text"
#for each of these, we can set specific parameters such as color (col), line width (lwd), text size multiplier (cex).
#you can add additional parameters to the list below.
#you could also save different themes for different kinds of graphs (or different projects)
#you can also modify and run these commands one at a time if you just want to change one thing
mytheme$fontsize$text = 18 #sets font for all text
mytheme$par.ylab.text$cex = 1.3 #sets multiplier for y label text
mytheme$par.xlab.text$cex = 1.1 #sets multiplier for x label text
mytheme$add.text$cex = 1.1 #sets multiplier added text, such as legends
mytheme$axis.line$lwd = 3 #sets axis line width
mytheme$background$col = "transparent" #This is the background color of the plot. I don't advise changing it. If you do, make it a light color.
mytheme$panel.background$col = "transparent"
mytheme$plot.polygon$col = "mediumorchid4" #this sets colors for histogram bars
mytheme$plot.polygon$lwd = 2 #sets line width of histogram
mytheme$box.rectangle$fill = "mediumorchid4" #these set boxplot parameters
mytheme$box.rectangle$lwd = 2
mytheme$box.rectangle$col = "black"
mytheme$box.umbrella$col = "black"
mytheme$box.umbrella$lwd = 2
mytheme$box.umbrella$lty = 1
mytheme$box.dot$col = "black"
mytheme$box.dot$pch = 16
mytheme$box.dot$cex = 1.5
mytheme$plot.symbol$col = "mediumorchid4" #these are the outliers in a boxplot
mytheme$plot.symbol$pch = 16
mytheme$plot.symbol$cex = 1.2
mytheme$superpose.symbol$pch <- c(15,16,17,18,19) #sets symbol type for xyplot
mytheme$superpose.symbol$col <- c("purple","orange","blue","black", "magenta") #sets colors of symbols
mytheme$superpose.symbol$cex <- 1.2 #sets multiplier for symbol size
mytheme$superpose.line$cex = 3 #sets line width for xyplots
mytheme$superpose.line$col = c("purple","orange","blue","black", "magenta")
mytheme$strip.background$col = "transparent" #strips on mutlipanel boxplot labels, etc.
mytheme$strip.border$lwd = 3
require(mosaic) # be sure to install and load the usual packages if you have not already
## Loading required package: mosaic
## Warning: package 'mosaic' was built under R version 3.2.5
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.2.5
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.2.5
## Loading required package: mosaicData
## Warning: package 'mosaicData' was built under R version 3.2.5
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 3.2.5
##
## The 'mosaic' package masks several functions from core packages in order to add additional features.
## The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
##
## mean
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cov, D, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
require(datasets)
require(lattice)
This is a plot using the default settings. I borrowed the basic code from Drew Kerkhoff’s KickStart data RPub: http://rpubs.com/kerkhoffa/KickstartGraphics
xyplot(cesd~mcs, groups=female, data=HELPrct, type=c("p","r"), xlab="Mental component score", ylab="Depression score", auto.key=list(title="Sex", text=c("male","female"), corner=c(0,0)))
This is a plot using the “mytheme” settings. I also changed the y axis scale and moved the legend into the upper right corner.
xyplot(cesd~mcs, groups=female, data=HELPrct, type=c("p","r"), xlab="Mental component score", ylab="Depression score", auto.key=list(title="Sex", text=c("male","female"), corner=c(1,1)), par.settings = "mytheme", ylim = c(0,80))
Suppose we want a smaller font on the y label. This can be changed by modifying the par settings before plotting.
mytheme$par.ylab.text$cex = 1.1 #sets multiplier for y label text
xyplot(cesd~mcs, groups=female, data=HELPrct, type=c("p","r"), xlab="Mental component score", ylab="Depression score", auto.key=list(title="Sex", text=c("male","female"), corner=c(1,1)), par.settings = "mytheme", ylim = c(0,80))