install.packages("plotly")
library(plotly)
sample_sheet <- read.csv("workshop_data/sample_sheet.csv")
sample_sheet
# Already wrangled data for plotting
load("workshop_data/workshop_data.RData")
# create a figures directory
dir.create("figures", showWarnings = FALSE)
# Optional (but very useful) set-up your condition colors as a variable
condition_colors <- c("WT" = "#E23E57", "KO" = "#3F72AF")
#Figure 1: Boxplot per sample - QC
# the start of ggplot - data + mapping
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition ))
# BoxPlot -----------------------------------------------------------------
# the most basic plot - adding layers (this includes geom)
ggplot(expr, mapping = aes(x = sample_id, y= expression, fill = "red")) +
geom_boxplot()
# do we want to change the aes of outliers?
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red")
#how about specifying the condition colors?
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors)
# now lets change the labels
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
)
# lets change the labs positions - start by adding a theme layer
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
) +
theme_minimal()
# then lets manipulate the aes of the text from inside the theme
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45,hjust = 1)
)
#a couple more changes on box color opacity for cohesiveness...
boxplot <- ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition , colour = condition)) +
geom_boxplot(alpha= 0.4,outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "black") +
scale_fill_manual(values = condition_colors) +
scale_colour_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition",
colour ="Condition"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45,hjust = 1)
)
ggsave(filename = "figures/boxplot_workshop.png",plot = boxplot)
# Volcano Plot ------------------------------------------------------------
volcano_colors <- c("Down in KO" = "#4647AE", "NS" = "grey80", "Up in KO" = "darkred")
#Starting from the most basic plot - careful here as geom_point uses colour, not fill.
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4)
# changing color values
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors)
# Adding ab lines
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40")
# changing labs aes
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
)
# adding theme and changing title position
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
)
# Volcano Plot with Gene Labels -------------------------------------------
# Sometimes we want to visualize specific data points within our plots.
# lets see an example using the previous volcano plot
genes_interest <- c("gene_2791","gene_1358","gene_4416","gene_1061")
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label_repel(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label_repel(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
genes_interest <- c("gene_2791","gene_1358","gene_4416","gene_1061")
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label_repel(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
sample_sheet <- read.csv("workshop_data/sample_sheet.csv")
sample_sheet
# Already wrangled data for plotting
load("workshop_data/workshop_data.RData")
# create a figures directory
dir.create("figures", showWarnings = FALSE)
# Optional (but very useful) set-up your condition colors as a variable
condition_colors <- c("WT" = "#E23E57", "KO" = "#3F72AF")
#Figure 1: Boxplot per sample - QC
# the start of ggplot - data + mapping
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition ))
# BoxPlot -----------------------------------------------------------------
# the most basic plot - adding layers (this includes geom)
ggplot(expr, mapping = aes(x = sample_id, y= expression, fill = "red")) +
geom_boxplot()
# do we want to change the aes of outliers?
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red")
#how about specifying the condition colors?
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors)
# now lets change the labels
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
)
# lets change the labs positions - start by adding a theme layer
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
) +
theme_minimal()
# then lets manipulate the aes of the text from inside the theme
ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition)) +
geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "red") +
scale_fill_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45,hjust = 1)
)
#a couple more changes on box color opacity for cohesiveness...
boxplot <- ggplot(expr,mapping = aes(x = sample_id, y = expression, fill = condition , colour = condition)) +
geom_boxplot(alpha= 0.4,outlier.size = 0.2, outlier.alpha = 0.3, outlier.colour = "black") +
scale_fill_manual(values = condition_colors) +
scale_colour_manual(values = condition_colors) +
labs(
title = "KO vs WT Expression",
x = NULL,
y = "Expression",
fill = "Condition",
colour ="Condition"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45,hjust = 1)
)
ggsave(filename = "figures/boxplot_workshop.png",plot = boxplot)
# Volcano Plot ------------------------------------------------------------
volcano_colors <- c("Down in KO" = "#4647AE", "NS" = "grey80", "Up in KO" = "darkred")
#Starting from the most basic plot - careful here as geom_point uses colour, not fill.
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4)
# changing color values
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors)
# Adding ab lines
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40")
# changing labs aes
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
)
# adding theme and changing title position
ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
)
# Volcano Plot with Gene Labels -------------------------------------------
# Sometimes we want to visualize specific data points within our plots.
# lets see an example using the previous volcano plot
genes_interest <- c("gene_2791","gene_1358","gene_4416","gene_1061")
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label_repel(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
?geom_label_repel
library(ggrepel)        # label manipulation
volcano <- ggplot(volcano, mapping = aes(x = log2FoldChange, y= -log10(padj), colour = diffexpr)) +
geom_point(alpha = 0.4) +
scale_colour_manual(values = volcano_colors) +
geom_hline(yintercept = -log10(p_cut), linetype = "dashed", colour = "grey40") +
geom_vline(xintercept = c(-fc_cut, fc_cut), linetype = "dashed", colour = "grey40") +
labs(
title = "Volcano Plot",
x = expression(log[2]~"FC"),
y = expression("-Log"[10]*"(padj)"),
colour = NULL
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_label_repel(
show.legend = FALSE, #this...if TRUE creates a symbol on top of the circle legend
data = volcano[volcano$gene_name %in% genes_interest,],
aes(label = gene_name),
size = 3,
min.segment.length = 0
)
# lets make the volcano plot interactive:
ggplotly(volcano)
# lets make the volcano plot interactive:
interactive_volcano <- ggplotly(volcano)
View(expr)
View(volcano_labels)
View(expr)
View(de_sig)
View(read_summary)
View(sample_sheet)
load("~/Documents/RStudio_Projects/ggplot_workshop/workshop_data/workshop_data.RData")
View(volcano)
write.csv(volcano,"volcano.csv")
volcano <- read.csv("volcano.csv")
View(volcano)
save.image("~/Documents/RStudio_Projects/ggplot_workshop/workshop_data/workshop_data.RData")
??cowplot
