Kenny’s guide to ggplot2
Preparation
Packages
library(data.table)
library(ggplot2)
library(patchwork)
library(extrafont)
extrafont::loadfonts(device='win', quiet=TRUE)
Data
my.data <- 'D:/Research/DR - 2021/Figures/remasters/fig_1and2_data.csv' # set path
dt <- data.table(read.csv(my.data)) # load data
Functions
f.scale <- function(x) sprintf("%.2f", x) # two decimal formatting
Variables
dt[,color.cat:='other'][lob %in% c('pauto','ho'),color.cat:=lob] # create color category
color.lobs <- c('pauto'='lightblue','ho'='lightgreen','other'='white') # map colors
Plotting
Create the basic plot
In this example, I reorder the lob by mean of lr and set fill color based on color.cat.
fig <- ggplot(dt, aes(x=reorder(lob,lr,fun=mean), y=lr, fill=color.cat))
Add a plot
fig <- fig + geom_boxplot(alpha=1, width=0.6, outlier.alpha=0.2)
First things first. The theme object will be visited later, but this will make it so much better.
fig <- fig + theme_bw()
Basic manipulations outside of theme
Set axis labels and title
fig <- fig + labs(
title = 'Loss ratios vary across lines of business',
y = 'Loss ratio',
x = 'Line of business'
)
Change the perspective and axis labels
fig <- fig + scale_y_continuous(expand=c(0,0), labels=f.scale) + coord_cartesian(ylim=c(0,2.1))# + scale_x_discrete(labels=lob.names)
Change the colors
fig <- fig + scale_fill_manual(values=color.lobs)
Add means
fig <- fig + stat_summary(fun=mean, geom='point', shape=21, size=4, color='black', fill='white')
Theme modifiers
Legend
fig <- fig + theme(legend.position = 'none')
Title and text
fig <- fig + theme(
# main title formatting
plot.title = element_text(size=20, hjust=0, lineheight=1.2, margin=margin(t=10,r=0,b=10,l=0)),
# axis title formatting and placement
axis.title.x=element_blank(),
axis.title.y=element_text(size=12, color='black', margin=margin(t=0, r=12, b=0, l=0)),
# axis text formatting and placement
axis.text.x=element_text(angle=60, hjust=1, vjust=1, size=11, color='black', margin=margin(t=5)),
axis.text.y=element_text(size=12, color='black'),
)
Background and panel components
fig <- fig + theme(
# plot area setup
panel.border = element_blank(),
panel.background = element_rect(fill='#fffcc7', color=NA),
# margin and color "around" the plot
plot.background = element_rect(fill='transparent', color=NA), # color is the border color
plot.margin = unit(c(1,1,1,1),'cm'),
)
Axes and gridlines
fig <- fig + theme(
# axis setup
axis.line.x = element_line(color='darkgrey', linetype='solid', size=0.5),
axis.line.y = element_line(color='darkgrey', linetype='solid', size=0.5),
# tick marks
axis.ticks.length.y = unit(.5, 'mm'),
axis.ticks.length.x = unit(0, 'mm'),
# major and minor gridlines
panel.grid.major.x = element_line(linetype='solid', color='transparent'),
panel.grid.minor.x = element_line(linetype='solid', color='transparent'),
panel.grid.major.y = element_line(linetype='solid', color='blue', size=0.2),
panel.grid.minor.y = element_line(linetype='solid', color='transparent'),
)
// add bootstrap table styles to pandoc tables function bootstrapStylePandocTables() { $('tr.odd').parent('tbody').parent('table').addClass('table table-condensed'); } $(document).ready(function () { bootstrapStylePandocTables(); });