Background
Chromosome-arm calls can be represented as a cell-by-arm heatmap. This workflow summarizes those calls as arm-level bar plots.
Goal
Convert the cell-by-arm heatmap into chromosome-arm bar plots. Both representations use chromosome arms on the x-axis.
Download original image: Cell-by-chromosome-arm heatmap for the 14-day dataset
Convert heatmap dataframe to long format
final_mtx_filtered_14_long <- tidyr::pivot_longer(final_mtx_filtered_14d,
cols = colnames(final_mtx_filtered_14d),
names_to = "amplicon",
values_to = "ploidy")
final_mtx_filtered_14_long$ploidy <- factor(final_mtx_filtered_14_long$ploidy, levels = c(0,1,2))
final_mtx_filtered_14_long$amplicon <- factor(final_mtx_filtered_14_long$amplicon, levels = AmpliconList_for_39SNPs)
Visualization
pdf(file = "PDF/Barplot/14d.pdf", width = 30,height = 20)
final_mtx_filtered_14d_long %>%
dplyr::count(amplicon, ploidy) %>%
tidyr::complete(amplicon, ploidy, fill = list(n = 0)) %>% # Complete missing combinations of amplicon and ploidy with 0 counts
ungroup() %>%
group_by(amplicon) %>%
mutate(pct = (n / sum(n)) * 100) %>%
ungroup() %>%
ggplot() +
aes(amplicon, pct, fill = ploidy) +
geom_bar(stat = "identity") +
ylab("Cell number (%)") +
xlab("Amplicons") +
ggrepel::geom_text_repel(aes( label = paste0(sprintf("%1.2f", pct),"%")),
position = position_stack(vjust = 0.5),
direction="y",
hjust=0.5,
box.padding=0.1,
color = "white",
size = 8) +
ggtitle("Ploidy composition of each amplicon in 14d data (511 consistent cells)") +
theme(text = element_text(size = 35),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
scale_fill_manual(values = c("#000000","#0080FF","#FF9933"),
name = "Ploidy")
dev.off()
Compare one chromosome arm across three datasets
pdf(file = "PDF/Barplot/per_amplicon.pdf", width = 10,height = 8)
###
for (amplicon_subset in AmpliconList_for_39SNPs) {
df <- data.frame(row.names = 1:3)
####
for (data in c("2N","8d","14d")) {
matrix <- get(paste0("final_mtx_filtered_",data,"_long"))
matrix_subset_pct <-
matrix %>%
dplyr::count(amplicon, ploidy) %>%
tidyr::complete(amplicon, ploidy, fill = list(n = 0)) %>% # Complete missing combinations of amplicon and ploidy with 0 counts
ungroup() %>%
group_by(amplicon) %>%
mutate(pct = (n / sum(n)) * 100) %>%
ungroup() %>%
subset(amplicon == amplicon_subset) %>%
select(pct)
colnames(matrix_subset_pct) = c(paste0("pct_", data))
df <- cbind(df, matrix_subset_pct)
}
####
# output df
df$ploidy = as.factor(c(0,1,2))
assign(paste0("pct_of_", amplicon_subset) , df,envir = .GlobalEnv)
# visualization
# preprocess the data format
matrix <- get(paste0("pct_of_", amplicon_subset))
long_matrix <- matrix %>% tidyr::pivot_longer(cols = c(-ploidy),
names_to = "dataset",
values_to = "percentage")
long_matrix$dataset <- factor(long_matrix$dataset, levels = c("pct_2N","pct_8d","pct_14d"))
plot <-
ggplot(long_matrix) +
aes(dataset, percentage, fill = ploidy) +
geom_bar(stat = "identity") +
ylab("Cell number (%)") +
xlab("Datasets") +
ggrepel::geom_text_repel(aes(label = paste0(sprintf("%1.2f", percentage),"%")),
position = position_stack(vjust = 0.5),
direction="y",
hjust=0.5,
box.padding=0.1,
color = "white",
size = 8) +
ggtitle(paste0("Ploidy composition of ", amplicon_subset," of each dataset")) +
theme(text = element_text(size = 20),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
scale_fill_manual(values = c("#000000","#0080FF","#FF9933"),
name = "Ploidy")
print(plot)
rm(df)
}
dev.off()
An example section of the resulting plot.