0

Line graph

enter image description here

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.

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
Hammonje
  • 1
  • 1
  • 1
    Welcome to SO! One option would be to use filter your data and use two `geom_line`. The first to use only the data for the black lines, the second for the red one. Doing so will automatically put the red ones on top. For more help please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data shared via `dput()`. – stefan Mar 12 '23 at 17:45
  • `ggplot(dplyr::arrange(gene_list_line_FC_mat, Label), aes(x=Timepoint, y=FC, group=Gene)) + .... etc` might be enough -- you want the Label = TRUE to be fed into ggplot last so they get plotted on top of the the black lines. – Jon Spring Mar 12 '23 at 23:33
  • ggplot(gene_list_line_FC_mat, aes(x=Timepoint, y=FC, group=Gene)) + geom_line(data=gene_list_line_FC_mat[gene_list_line_FC_mat$Label==F,], linewidth=1, color='grey', show.legend=F)+ geom_line(data=gene_list_line_FC_mat[gene_list_line_FC_mat$Label==T,], linewidth=1.5, color='red', show.legend=F)+ 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, size = 6)+ – Hammonje Mar 14 '23 at 00:32

0 Answers0