# NOT RUN {
# }
# NOT RUN {
set.seed(12345);
simple.data <- data.frame(
x = sample(1:15, 10),
y = LETTERS[1:10]
);
# Simple example
create.barplot(
filename = paste0(tempdir(), '/Barplot_Simple.tiff'),
formula = x ~ y,
data = simple.data,
yat = seq(0,16,2)
);
# set up the data
total.counts <- apply(SNV[1:15], 2, function(x){ mutation.count <- (30 - sum(is.na(x)))});
count.nonsyn <- function(x){
mutation.count <- length(which(x == 1));
}
nonsynonymous.SNV <- apply(SNV[1:15], 2, count.nonsyn);
other.mutations <- total.counts - nonsynonymous.SNV;
# subset the first fifteen samples
barplot.data <- data.frame(
samples = rep(1:15, 2),
mutation = c(rep('nonsynonymous', 15), rep('other',15)),
values = c(nonsynonymous.SNV, other.mutations),
sex = rep(patient$sex[1:15], 2),
stage = rep(patient$stage[1:15], 2),
msi = rep(patient$msi[1:15], 2)
);
# Minimal input
create.barplot(
filename = paste0(tempdir(),'/Barplot_Minimal_Input.tiff'),
formula = values ~ samples ,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Minimal input',
# Editing the metadata
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Axes labels & limits
create.barplot(
filename = paste0(tempdir(),'/Barplot_Custom_Axes.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Axes labels & limits',
# Setting axes labels
xlab.lab = 'Sample',
ylab.lab = 'Nonsynonymous SNVs',
# Setting y-axis limits and tick-mark locations
ylimits = c(0,30),
yat = seq(0,30,5),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Font size and font face
create.barplot(
filename = paste0(tempdir(), '/Barplot_Font_Changes.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Font changes',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
# Changing font sizes
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
# Changing font type
xaxis.fontface = 1,
yaxis.fontface = 1,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Sorting data
create.barplot(
filename = paste0(tempdir(), '/Barplot_Sorted.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Sorted bars',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Order bars either by 'increasing' or 'decreasing'
sample.order = 'decreasing',
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Colour changes
sex.colours <- replace(as.vector(barplot.data$sex), which(barplot.data$sex == 'male'),'dodgerblue');
sex.colours <- replace(sex.colours, which(barplot.data$sex == 'female'), 'pink');
create.barplot(
filename = paste0(tempdir(),'/Barplot_Colour_Changes.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Colour changes',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Colour bars based on sex
col = sex.colours,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Legend
create.barplot(
filename = paste0(tempdir(),'/Barplot_Legend.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Legend',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
col = sex.colours,
# Adding legend to explain bar colour-coding
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Grouped barplot
create.barplot(
filename = paste0(tempdir(),'/Barplot_Grouped.tiff'),
formula = values ~ samples,
data = barplot.data,
main = 'Grouped bar chart',
xlab.lab = 'Samples',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Setting groups
groups = mutation,
col = default.colours(12, is.greyscale = FALSE)[11:12],
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 2,
fill = default.colours(12, is.greyscale = FALSE)[11:12]
),
text = list(
lab = c('Nonsynonymous SNV','Other SNV')
),
padding.text = 3,
cex = 1
)
),
x = 0.55,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Grouped labels
create.barplot(
filename = paste0(tempdir(),'/Barplot_Grouped_Labels.tiff'),
formula = values ~ samples,
data = barplot.data,
main = 'Grouped labels',
xlab.lab = 'Samples',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Setting groups
groups = mutation,
col = default.colours(12, is.greyscale = FALSE)[11:12],
# Grouped labels
xaxis.lab = rep(c('nonsynonymous', 'other'), 15),
xaxis.rot = 90,
group.labels = TRUE,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Stacked barplot
create.barplot(
filename = paste0(tempdir(),'/Barplot_Stacked.tiff'),
formula = values ~ samples,
data = barplot.data,
main = 'Stacked bar chart',
xlab.lab = 'Samples',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
groups = mutation,
col = default.colours(12, is.greyscale = FALSE)[11:12],
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 2,
# reverse order to match stacked bar order
fill = rev(default.colours(12, is.greyscale = FALSE)[11:12])
),
text = list(
# reverse order to match stacked bar order
lab = rev(c('Nonsynonymous SNV','Other SNV'))
),
padding.text = 3,
cex = 1
)
),
x = 0.55,
y = 0.95
)
),
# Changing the plot from a grouped plot to a stacked plot
stack = TRUE,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Panel organization
create.barplot(
filename = paste0(tempdir(),'/Barplot_Panel_Layout.tiff'),
# Setting the panel layout
formula = values ~ samples | mutation,
data = barplot.data,
main = 'Panel layout',
xlab.lab = 'Samples',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Panel organization 2
create.barplot(
filename = paste0(tempdir(),'/Barplot_Panel_Layout_2.tiff'),
formula = values ~ samples | mutation,
data = barplot.data,
main = 'Panel layout',
xlab.lab = 'Samples',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Adjusting the panel layout
layout = c(1,2),
y.spacing = 1,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Covariates
# Note: Covariates can also be created using the create.multiplot function
# set covariate colour schemes
covariate.colours.sex <- as.character(barplot.data$sex);
covariate.colours.sex[covariate.colours.sex == 'male'] <- 'dodgerblue';
covariate.colours.sex[covariate.colours.sex == 'female'] <- 'pink';
covariate.colours.stage <- as.character(barplot.data$stage);
covariate.colours.stage[covariate.colours.stage == 'I'] <- 'plum1';
covariate.colours.stage[covariate.colours.stage == 'II'] <- 'orchid1';
covariate.colours.stage[covariate.colours.stage == 'III'] <- 'orchid3';
covariate.colours.stage[covariate.colours.stage == 'IV'] <- 'orchid4';
covariate.colours.msi <- as.character(barplot.data$msi);
covariate.colours.msi[covariate.colours.msi == 'MSS'] <- 'chartreuse4';
covariate.colours.msi[covariate.colours.msi == 'MSI-High'] <- 'chartreuse2';
# create object to draw covariates
covariates.object <- list(
rect = list(
col = 'white',
fill = covariate.colours.sex,
lwd = 1.5
),
rect = list(
col = 'white',
fill = covariate.colours.stage,
lwd = 1.5
),
rect = list(
col = 'white',
fill = covariate.colours.msi,
lwd = 1.5
)
);
# see BoutrosLab.plotting.general::covariates.grob() for more information
covariate.object.grob <- covariates.grob(
covariates = covariates.object,
ord = c(1:15),
side = 'top',
size = 0.8
);
# Create legend to explain covariates
covariates.legends <- list(
legend = list(
colours = c('dodgerblue','pink'),
labels = c('male','female'),
title = 'Sex',
border = 'white'
),
legend = list(
colours = c('plum1', 'orchid1', 'orchid3', 'orchid4'),
labels = c('I','II','III','IV'),
title = 'Stage',
border = 'white'
),
legend = list(
colours = c('chartreuse4','chartreuse2'),
labels = c('MSS','MSI-High'),
title = 'MSI',
border = 'white'
)
);
# see BoutrosLab.plotting.general::legend.grob() for more information
covariate.legend.grob <- legend.grob(
legends = covariates.legends,
title.just = 'left'
);
create.barplot(
filename = paste0(tempdir(),'/Barplot_Covariates.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Covariates',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
yaxis.fontface = 1,
# removing x-axis formatting to give space to covariates
xaxis.tck = 0,
xaxis.lab = rep('',15),
xaxis.cex = 0,
# covariates
legend = list(
bottom = list(fun = covariate.object.grob),
right = list(fun = covariate.legend.grob)
),
key = list(
x = 1,
y = -0.028,
text = list(
lab = c('Sex','Stage','MSI')
),
padding.text = 1
),
bottom.padding = 4,
description = 'Barplot created by BoutrosLab.plotting.general'
);
create.barplot(
filename = paste0(tempdir(),'/Barplot_Auto_legend.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Covariates',
ylab.lab = 'Mutations',
ylimits = c(0,30),
yat = seq(0,30,5),
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
yaxis.fontface = 1,
# removing x-axis formatting to give space to covariates
xaxis.tck = 0,
xaxis.lab = rep('',15),
xaxis.cex = 0,
# covariates
legend = list(
inside = list(fun = covariate.legend.grob)
),
bottom.padding = 4,
inside.legend.auto = TRUE,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Horizontal orientation
create.barplot(
filename = paste0(tempdir(),'/Barplot_Horizontal.tiff'),
# switch formula order
formula = samples ~ values,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Plot horizontally',
# Adjusting the panel layout
plot.horizontal = TRUE,
# covariates
legend = list(
inside = list(fun = covariate.legend.grob)
),
inside.legend.auto = TRUE,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Change bar thickness and add text labels
create.barplot(
filename = paste0(tempdir(),'/Barplot_Text_Labels.tiff'),
# switch formula order
formula = samples ~ values,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Text labels and thin bars',
# Adjusting the panel layout
plot.horizontal = TRUE,
box.ratio = 0.6,
add.text = TRUE,
text.x = 27.75,
text.y = 1:15,
text.labels = barplot.data[barplot.data$mutation == 'nonsynonymous','values'],
text.cex = 0.8,
text.fontface = 'italic',
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Error bars
error.data <- data.frame(
genes = rownames(microarray)[1:15],
values = apply(microarray[1:15,1:58], 1, mean),
error = apply(microarray[1:15,1:58], 1, sd)
);
create.barplot(
filename = paste0(tempdir(),'/Barplot_Error_Bars.tiff'),
# needs sequential x-axis
formula = values ~ 1:15,
data = error.data,
y.error.up = error.data$error,
xaxis.lab = error.data$genes,
main = 'Error bars',
xlab.lab = 'Gene',
ylab.lab = 'Change in Expression',
ylimits = c(0,14),
yat = seq(0,14,2),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.rot = 45,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
description = 'Barplot created by BoutrosLab.plotting.general'
);
create.barplot(
filename = paste0(tempdir(),'/Barplot_Error_Bars_Horizontal.tiff'),
# needs sequential x-axis
formula = values ~ 1:15,
data = error.data,
y.error.up = error.data$error,
yaxis.lab = error.data$genes,
plot.horizontal = TRUE,
main = 'Error bars',
xlab.lab = 'Gene',
ylab.lab = 'Change in Expression',
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.rot = 45,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Grid lines
create.barplot(
filename = paste0(tempdir(),'/Barplot_Gridlines.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Gridlines',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Grid lines
add.grid = TRUE,
xgrid.at = seq(0,15,2),
col = sex.colours,
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Grid lines 2
create.barplot(
filename = paste0(tempdir(),'/Barplot_Gridlines_GreyBG.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Gridlines & grey background',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Grid lines
background.col = 'grey85',
add.grid = TRUE,
xgrid.at = seq(0,15,2),
col = sex.colours,
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Labels
create.barplot(
filename = paste0(tempdir(),'/Barplot_Labels.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Labels',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Labels
text.above.bars = list(
labels = c('*','27','15','*'),
padding = 0.75,
bar.locations = c(1, 3, 12, 14),
rotation = 0
),
col = sex.colours,
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# lines
create.barplot(
filename = paste0(tempdir(),'/Barplot_Lines.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Lines',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# Lines
sample.order = 'increasing',
line.func = function(x) {0.1*x**2},
line.from = 0,
line.to = 16,
line.col = 'darkgrey',
abline.h = 10,
abline.col = 'red',
col = sex.colours,
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Background rectangle
create.barplot(
filename = paste0(tempdir(),'/Barplot_Bg_Rectangle.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Background rectangle',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
sample.order = 'increasing',
# Background rectangle
add.rectangle = TRUE,
xleft.rectangle = seq(0.5, 14.5, 2),
ybottom.rectangle = 0,
xright.rectangle = seq(1.5, 15.5, 2),
ytop.rectangle = 30,
col.rectangle = 'lightgrey',
col = sex.colours,
legend = list(
inside = list(
fun = draw.key,
args = list(
key = list(
points = list(
col = 'black',
pch = 22,
cex = 3,
fill = c('dodgerblue', 'pink')
),
text = list(
lab = c('Male','Female')
),
padding.text = 5,
cex = 1
)
),
# Positioning legend on plot
x = 0.75,
y = 0.95
)
),
description = 'Barplot created by BoutrosLab.plotting.general'
);
# Raster
create.barplot(
filename = paste0(tempdir(),'/Barplot_with_raster.tiff'),
formula = values ~ samples,
data = barplot.data[barplot.data$mutation == 'nonsynonymous',],
main = 'Raster fill',
xlab.lab = 'Samples',
ylab.lab = 'Nonsynonymous SNVs',
ylimits = c(0,30),
yat = seq(0,30,5),
xaxis.cex = 1,
yaxis.cex = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
xaxis.fontface = 1,
yaxis.fontface = 1,
# filling bars with raster
raster = 1:10/10,
raster.just = 'bottom',
description = 'Description of image here'
);
# Nature format
create.barplot(
filename = paste0(tempdir(),'/Barplot_Nature_style.tiff'),
formula = x ~ y,
data = simple.data,
yat = seq(0,16,2),
main = 'Nature style',
# set style to Nature
style = 'Nature',
# demonstrating how to italicize character variables
ylab.lab = expression(paste('italicized ', italic('a'))),
# demonstrating how to create en-dashes
xlab.lab = expression(paste('en dashs: 1','\u2013', '10'^'\u2013', ''^3))
);
# Left Justified Example
create.barplot(
filename = paste0(tempdir(),'/Barplot_TwoTopLabelsLeftJustified.tiff'),
formula = x ~ y,
data = simple.data,
yat = seq(0,16,2),
ylab.label = NULL,
# set top label details
xlab.top.label = 'Sample Label',
xlab.top.cex = 1.5,
xlab.top.x = -0.125,
xlab.top.y = 0.5,
xlab.top.just = 'left',
# set main label details
main = 'Sample Main',
main.just = 'left',
main.x = 0,
main.y = 0.6,
top.padding = 1
);
# }
Run the code above in your browser using DataLab