# NOT RUN {
# }
# NOT RUN {
set.seed(12345);
simple.data <- data.frame(
x = rnorm(800),
y = rnorm(800)
);
create.scatterplot(
filename = paste0(tempdir(), '/Scatterplot_Simple.tiff'),
formula = y ~ x,
data = simple.data
);
scatter.data <- data.frame(
sample.one = microarray[1:800,1],
sample.two = microarray[1:800,2],
chr = microarray$Chr[1:800]
);
# Minimal Input
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Minimal_Input.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Minimal Input',
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Axes & Labels
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Axes_Labels.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Axes & Labels',
# Axes and labels
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Plotting space
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Plotting_Space.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Plotting Space',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
# change axes accordingly
xat = 2 ** (0:4),
yat = seq(0, 16, 2),
# change axes range accordingly
xlimits = c(2 ** 0,16),
ylimits = c(0, 15),
# format labels
xaxis.lab = c(
expression('2'^'0'),
expression('2'^'1'),
expression('2'^'2'),
expression('2'^'3'),
expression('2'^'4')
),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
# Transform the x-axis into log-2 space
xaxis.log = 2,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Colour & Plotting Character
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Colour_Pch.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Colour & Pch',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
# setting the colour
col = default.colours(2)[2],
# setting the plotting character type & size
pch = 21,
cex = 1.5,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Colour depth
# create colour scheme to illustrate adding a colourkey
chr.palette <- colour.gradient(default.colours(2)[2], 800);
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Colour_Depth.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Colour Depth',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
# setting the colour
col = chr.palette,
# setting the plotting character type & size
pch = 19,
cex = 1,
# adding key for colours
key.top = 1.5,
legend = list(
bottom = list(
fun = draw.colorkey,
args = list(
key = list(
col = chr.palette,
at = 1:800,
tick.number = 3,
space = 'bottom',
size = 1,
width = 1.25,
height = 1,
labels = list(
labels = 1:3,
cex = 1,
at = c(1, which(scatter.data$chr == 2)[1], which(scatter.data$chr == 3)[1])
)
)
)
)
),
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Groups & Legend
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Groups_Legend.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Groups & Legend',
# using arbitrary groups for the sake of illustration
groups = scatter.data$chr,
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
col = default.colours(3),
# Adding legend for groups
key = list(
text = list(
lab = c('1','2','3'),
cex = 1,
col = 'black'
),
points = list(
pch = 19,
col = default.colours(3),
cex = 1
),
x = 0.04,
y = 0.95,
padding.text = 2
),
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Correlation Key
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Correlation_Key.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Correlation Key',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
col = 'black',
pch = 21,
# Adding correlation key
legend = list(
inside = list(
fun = draw.key,
args = list(
key = get.corr.key(
x = scatter.data$sample.one,
y = scatter.data$sample.two,
label.items = c('spearman','spearman.p','kendall','beta1'),
alpha.background = 0,
key.cex = 1
)
),
x = 0.04,
y = 0.95,
corner = c(0,1)
)
),
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Panel Organization
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Panel.tiff'),
formula = sample.two ~ sample.one | chr,
data = scatter.data,
main = 'Panel',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
# set up panel layout
layout = c(1,3),
yrelation = 'free',
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Covariates
cov.colours <- as.character(microarray$Chr[1:800]);
cov.colours[cov.colours == '1'] <- default.colours(3, palette.type = 'chromosomes')[1];
cov.colours[cov.colours == '2'] <- default.colours(3, palette.type = 'chromosomes')[2];
cov.colours[cov.colours == '3'] <- default.colours(3, palette.type = 'chromosomes')[3];
cov <- list(
rect = list(
col = 'transparent',
fill = cov.colours
)
);
cov.grob <- covariates.grob(
covariates = cov,
ord = c(1:length(cov.colours)),
side = 'top',
size = 1
);
cov.legend <- list(
legend = list(
colours = default.colours(3, palette.type = 'chromosomes'),
labels = c('1', '2', '3'),
title = 'Chromosome',
border = 'transparent'
)
);
cov.legend.grob <- legend.grob(
legends = cov.legend
);
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Covariates.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Covariates',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
# Adding covariate & legend
legend = list(
bottom = list(fun = cov.grob),
right = list(fun = cov.legend.grob)
),
# Ensuring sufficient spacing for covariate
key.top = 3,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Error bars
error.data <- data.frame(
chr = (microarray$Start)[1:20],
values = apply(microarray[1:20,1:58], 1, mean),
error = apply(microarray[1:20,1:58], 1, sd)
);
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Error_Bars.tiff'),
formula = values ~ chr,
data = error.data,
main = 'Error Bars',
xlab.label = 'Base pair location on chromosome one',
ylab.label = 'Gene expression change',
# xat = seq(0, 16, 2),
yat = seq(0, 14, 2),
# xlimits = c(0, 15),
ylimits = c(0, 13),
# Format xaxes
xaxis.lab = c(
scientific.notation(0, 1),
scientific.notation(1000000, 1),
scientific.notation(2000000, 1),
scientific.notation(3000000, 1),
scientific.notation(4000000, 1),
scientific.notation(5000000, 1),
scientific.notation(6000000, 1),
scientific.notation(7000000, 1)
),
xaxis.rot = 90,
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 19,
col = 'black',
fill = 'transparent',
# Specifying error bars
error.bar.lwd = 1,
error.whisker.angle = 120,
y.error.up = error.data$error,
y.error.bar.col = 'black',
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Error_Bars_MultiColor.tiff'),
formula = values ~ chr,
data = error.data,
main = 'Error Bars',
xlab.label = 'Base pair location on chromosome one',
ylab.label = 'Gene expression change',
# xat = seq(0, 16, 2),
yat = seq(0, 14, 2),
# xlimits = c(0, 15),
ylimits = c(0, 13),
# Format xaxes
xaxis.lab = c(
scientific.notation(0, 1),
scientific.notation(1000000, 1),
scientific.notation(2000000, 1),
scientific.notation(3000000, 1),
scientific.notation(4000000, 1),
scientific.notation(5000000, 1),
scientific.notation(6000000, 1),
scientific.notation(7000000, 1)
),
xaxis.rot = 90,
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 19,
col = 'black',
fill = 'transparent',
# Specifying error bars
error.bar.lwd = 1,
error.whisker.angle = 120,
y.error.up = error.data$error,
y.error.bar.col = c('black','red','blue'),
description = 'Scatter plot created by BoutrosLab.plotting.general',
group.specific.colouring = FALSE
);
# Gridlines
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Gridlines.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Gridlines',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
# Adding gridlines
type = c('p','g'),
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# lines & background rectangle
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Lines_BG.tiff'),
formula = sample.two ~ sample.one,
data = scatter.data,
main = 'Lines & BG rectangle',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
type = c('p','g'),
# add xy line
add.xyline = TRUE,
xyline.lty = 3,
xyline.col = 'red',
xyline.lwd = 3,
# add background rectangle
add.rectangle = TRUE,
xleft.rectangle = which(scatter.data$chr == 2)[1]/800*15,
xright.rectangle = which(scatter.data$chr == 3)[1]/800*15,
ybottom.rectangle = 0,
ytop.rectangle = 15,
col.rectangle = 'grey',
alpha.rectangle = 0.5,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# attach lines to points
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Lines.tiff'),
formula = sample.two ~ sample.one | chr,
data = scatter.data,
main = 'Lines',
xlab.label = colnames(microarray[1]),
ylab.label = colnames(microarray[2]),
xat = seq(0, 16, 2),
yat = seq(0, 16, 2),
xlimits = c(0, 15),
ylimits = c(0, 15),
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
# attach lines
type = c('h','p'),
layout = c(1,3),
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# ROC curve
set.seed(123456);
class.values <- runif(50, 0, 1);
observed.values <- sample(c(0,1), size = 50, replace = TRUE);
cutoffs <- seq(1,0,-0.01);
tprs <- c();
fprs <- c();
for (c in cutoffs) {
roc.classification <- rep(0, length(class.values));
roc.classification[class.values >= c] <- 1;
roc.results <- table(
factor(roc.classification, levels = c(0,1)),
factor(observed.values, levels = c(0,1)),
dnn = c('pred', 'obs')
);
tprs <- c(tprs, roc.results[2,2] / (roc.results[2,2] + roc.results[1,2]));
fprs <- c(fprs, roc.results[2,1] / (roc.results[2,1] + roc.results[1,1]));
}
roc.data <- data.frame(cutoff = cutoffs, TPR = tprs, FPR = fprs);
points.x <- roc.data[match(c(0.25, 0.5, 0.75), roc.data$cutoff), 'FPR'];
points.y <- roc.data[match(c(0.25, 0.5, 0.75), roc.data$cutoff), 'TPR'];
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_ROC.tiff'),
formula = TPR ~ FPR,
data = roc.data,
main = 'ROC',
xlab.label = 'False positive rate',
ylab.label = 'True positive rate',
xaxis.cex = 1,
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
type = 's',
lwd = 3,
add.xyline = TRUE,
xyline.col = 'grey',
add.points = TRUE,
points.x = points.x,
points.y = points.y,
points.col = c('blue', 'darkgreen', 'red'),
add.text = TRUE,
text.labels = paste('cutoff = ', c(0.25, 0.5, 0.75), sep = ''),
#text.x = points.x - 0.14,
#text.y = points.y + 0.03,
text.x = points.x,
text.y = points.y,
text.guess.labels = TRUE,
text.guess.label.position = 155,
text.guess.radius.factor = 2.5,
description = 'Scatter plot created by BoutrosLab.plotting.general'
);
# Volcano Plots
fold.change <- apply(microarray[,1:29], 1, mean) - apply(microarray[,30:58], 1, mean);
fake.microarray <- microarray[,1:58] - log(mean(apply(microarray[,1:58],1, mean)));
fake.microarray[,30:58] <- fake.microarray[,30:58] + mean(fold.change);
fake.microarray[fake.microarray < 0] <- 0;
p.values <- apply(fake.microarray[,1:58], 1, function(x) {t.test(x=x[1:29],y=x[30:58])$p.value} );
fold.change <- apply(fake.microarray[, 1:29], 1, mean) - apply(fake.microarray[, 30:58], 1,mean);
p.values.adjusted <- p.adjust(p.values, 'fdr');
dot.colours <- vector(length=length(p.values));
dot.colours[p.values.adjusted < .05 & fold.change < 0] <- 'green';
dot.colours[p.values.adjusted < .05 & fold.change > 0] <- 'red';
dot.colours[p.values.adjusted > .05] <- 'black';
volcano.data <- data.frame(
p.values = -log10(p.values.adjusted),
fold.change = fold.change
);
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Volcano_Plot.tiff'),
formula = p.values ~ fold.change,
data = volcano.data,
col = dot.colours,
alpha = .5,
yat = c(0,2,4,6,8),
ylimits = c(-0.1,8.1),
yaxis.lab = expression(10^0,10^-2,10^-4,10^-6,10^-8),
yaxis.cex = 1.5,
xaxis.cex = 1.5,
xlab.label = 'foldChange',
ylab.label = 'pValues',
xlab.cex = 1.75,
ylab.cex = 1.75
);
# Automatic Labeling
interesting.fold.change <- (fold.change < -.9 | fold.change > .9);
interesting.p.value <- (-log10(p.values.adjusted) < 8 & -log10(p.values.adjusted) > 2);
interesting.points <- interesting.fold.change & interesting.p.value;
text.x <- fold.change[interesting.points];
text.y <- (-log10(p.values.adjusted))[interesting.points];
text.labels <- rownames(microarray)[interesting.points];
create.scatterplot(
filename = paste0(tempdir(),'/Scatterplot_Volcano_Plot_With_Labels.tiff'),
formula = p.values ~ fold.change,
data = volcano.data,
alpha = .5,
yat = c(0,2,4,6,8),
ylimits = c(-0.1,8.1),
xlimits = c(-1.5,1.5),
yaxis.lab = expression(10^0,10^-2,10^-4,10^-6,10^-8),
yaxis.cex = 1.5,
xaxis.cex = 1.5,
xlab.label = 'foldChange',
ylab.label = 'pValues',
xlab.cex = 1.75,
ylab.cex = 1.75,
add.text = TRUE,
text.x = text.x,
text.y = text.y,
text.labels = text.labels,
text.guess.labels = TRUE
);
# With line segments
line.data <- data.frame(
group = c('A','B','C'),
x = sample(1:10,3),
y = sample(1:10,3),
z = sample(1:10,3)
);
create.scatterplot(
(x+y+z) ~ group,
line.data,
filename = paste0(tempdir(),'/Scatterplot_with_LineSegments.tiff'),
cex = 0,
add.line.segments = TRUE,
line.start = list(
rep(0,nrow(line.data)),
line.data$x,
c(line.data$x + line.data$y)
),
line.end = list(
line.data$x,
c(line.data$x + line.data$y),
c(line.data$x + line.data$y + line.data$z)
),
line.col = list('red','blue','green'),
line.lwd = list(3,3,3)
);
lollipop.data <- data.frame(
y = seq(1,100,1),
x = rnorm(100)
);
create.lollipopplot(
filename = paste0(tempdir(),"/Lollipop_Simple.tiff"),
formula = x ~ y,
data = lollipop.data,
main = 'Lollipop plot',
xaxis.cex = 1,
xlimits = c(-1,102),
yaxis.cex = 1,
xaxis.fontface = 1,
yaxis.fontface = 1,
xlab.cex = 1.5,
ylab.cex = 1.5,
pch = 21,
col = 'black',
fill = 'transparent',
description = 'Scatter plot created by BoutrosLab.plotting.general',
regions.start = c(1,26,48),
regions.stop = c(15,35,72),
regions.labels = c("test 1", "test2", "test 3"),
regions.color = c("#66b3ff", "#5cd65c", "#ff3333")
);
# }
Run the code above in your browser using DataLab