Line graph
I have used this code to produce a line graph using ggplot2. Would like red lines to be on top of rest of lines for improved readability. Have spent several hours attempting to do so, but am unsuccessful.
Code:
#Plot linegraph of gene lists of interest (e.g. most common cluster genes) per cell type
for (j in 1:length(clusters)) {
#Lookup gene list
ind=match(rownames(gene_list)[gene_list$Cluster==clusters[j]], rownames(eset))
#Build matrix of avg common gene exp over time per cell type
gene_list_line_FC_mat=vector("list",length(cell_types_plot))
for (k in 1:length(cell_types_plot)) {
gene_list_line_FC_mat[[k]]=array(NA, dim=c(length(ind),ncol(eset_mean_D0FC[[cell_types_plot[k]]])+1),
dimnames=list(rownames(eset)[ind],c(0,tp)))
#Set D0 FC to 0
gene_list_line_FC_mat[[k]][,1]=0
gene_list_line_FC_mat[[k]][,-1]=exprs(eset_mean_D0FC[[cell_types_plot[k]]][ind,])
#Convert matrix to long format
gene_list_line_FC_mat[[k]]=data.frame(gene_list_line_FC_mat[[k]]) %>% tibble::rownames_to_column(var = "Gene") %>%
pivot_longer(-Gene, names_to='Timepoint', values_to='FC')
#Convert timepoint back to numeric
gene_list_line_FC_mat[[k]]$Timepoint=as.numeric(str_replace(gene_list_line_FC_mat[[k]]$Timepoint, 'X', ''))
#Add cell type column
gene_list_line_FC_mat[[k]]$Cell_type=cell_types_plot[k]
}
#Combine results from each cell type
gene_list_line_FC_mat=do.call(rbind, gene_list_line_FC_mat)
#Find top n genes based on FC for labeling (at tp_max in cell_type_max)
genes_label=gene_list_line_FC_mat$Gene[
gene_list_line_FC_mat$Timepoint==tp_max_label & gene_list_line_FC_mat$Cell_type==cell_type_max_label][
order(-gene_list_line_FC_mat$FC[gene_list_line_FC_mat$Timepoint==tp_max_label & gene_list_line_FC_mat$Cell_type==cell_type_max_label])[1:n_genes_label]]
genes_label=gene_list_line_FC_mat$Label=genes_label=gene_list_line_FC_mat$Gene %in% genes_label
#Change cell type to factor for plotting order
gene_list_line_FC_mat$Cell_type=factor(gene_list_line_FC_mat$Cell_type, levels=cell_types_plot)
#Plot
ggplot(gene_list_line_FC_mat, aes(x=Timepoint, y=FC, group=Gene)) +
geom_line(linewidth=1, aes(color=Label), show.legend=F)+
scale_colour_manual(values=c('TRUE'='red','FALSE'='black'))+
geom_label_repel(data=gene_list_line_FC_mat[gene_list_line_FC_mat$Timepoint==8 & gene_list_line_FC_mat$Label==T,], aes(label=Gene),
nudge_x=1, max.overlaps=10)+
theme_classic()+
scale_x_continuous(breaks=c(0,tp))+
xlab("Day post-infection")+
ylab("Mean FC (log2)")+
ggtitle(paste("Cluster",j))+
theme(plot.title = element_text(hjust = 0.5), text = element_text(size=20))+
facet_wrap(~Cell_type)
ggsave(file.path(module_filename,paste0(module_filename,"_gene_linegraph_TFS_MDM_cluster",j,".pdf")), width = 15, height = 10)
}
Tried many iterations of geom_line additions.