Commit faa2f05a authored by HaojiaWu's avatar HaojiaWu
Browse files

Update methods

parent d4d4b1be
Loading
Loading
Loading
Loading

R/plot_cell_fraction.R

0 → 100644
+79 −0
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Plot cell fractions across groups
#'
#' This function to show the cell fraction changes across groups.
#'
#' @param seu_obj A complete Seurat object
#' @param celltypes Cell types to be included in the dot plot. Default: all cell types.
#' @param groupby The group to show on x axis. One of the column names in meta.data.
#' @param show_replicate Whether or not to show the individual replicates from the graph. If TRUE, the replicate column names need to specify.
#' @param rep_colname The column name for biological replicates in the meta data.
#' @param strip.color Colors for the strip background
#' @return A ggplot object
#' @export

plot_cell_fraction<-function(
  seu_obj, 
  celltypes=NULL, 
  groupby, 
  show_replicate = FALSE, 
  rep_colname = NULL,
  strip.color = NULL
  ){
  meta_data<-seu_obj@meta.data
  meta_data$celltype<-as.character(Idents(seu_obj))
  groupby_level<-levels(seu_obj@meta.data[,groupby])
  if (is.null(groupby_level)){
    seu_obj@meta.data[,groupby] <-factor(seu_obj@meta.data[,groupby], levels = names(table(seu_obj@meta.data[,groupby])))
    groupby_level<-levels(seu_obj@meta.data[,groupby])
  } 
  if(!show_replicate){
    freq_df<-prop.table(table(meta_data[,"celltype"], meta_data[, groupby]), margin = 2)
    freq_df<-data.frame(freq_df)
    colnames(freq_df)[1:2]<-c("celltype","group")
    freq_df$Freq<-freq_df$Freq*100
  } else {
    if(is.null(rep_colname)){
      stop("Please specify the replicate colname in your meta data!")
    } else {
      meta_data$new_group<-paste(meta_data[,"celltype"], meta_data[,groupby], meta_data[, rep_colname], sep = "___")
      freq_df<-data.frame(table(meta_data$new_group))
      freq_df$Var1<-as.character(freq_df$Var1)
      freq_df$celltype <- as.character(lapply(X=strsplit(freq_df$Var1, split = "___"),FUN = function(x){x[[1]]}))
      freq_df$group <- as.character(lapply(X=strsplit(freq_df$Var1, split = "___"),FUN = function(x){x[[2]]}))
      freq_df$replicate <- as.character(lapply(X=strsplit(freq_df$Var1, split = "___"),FUN = function(x){x[[3]]}))
      total_cell<-aggregate(Freq ~ replicate, freq_df, sum)
      colnames(total_cell)[2]<-"Total"
      freq_df <- merge(freq_df, total_cell, by="replicate")
      freq_df$Freq<-freq_df$Freq*100/freq_df$Total
    }
  }
  freq_df$group <- factor(freq_df$group, levels = groupby_level)
  if(is.null(celltypes)){
    celltypes <- levels(seu_obj)
  }
  freq_df <- freq_df[freq_df$celltype %in% celltypes, ]
  freq_df$celltype <- factor(freq_df$celltype, levels = celltypes)
 p <- ggplot(freq_df, aes(group, Freq, fill=group))+
    geom_bar(position = "dodge",  stat = "summary", fun='mean', width = 0.6)+
    stat_summary(fun.data = mean_se, geom = "errorbar", width=0.2, size=1, color='midnightblue', alpha=0.8)+
    ylab('Percentage of cells')+xlab('')+
    scale_fill_manual(values = brewer.pal(8, 'Set2'))+
    theme(panel.background = element_rect(fill = "white", colour = "grey50"),
          strip.text = element_text(size = 12),axis.title = element_text(size = 14),
          axis.text.x = element_text(angle = 45, vjust = 1, hjust=1, size = 12),
          legend.position = "none", axis.text.y = element_text(size = 12),
          legend.text = element_text(size = 12), legend.title = element_text(size = 12, face = 'bold'), 
          plot.title = element_text(hjust =0.5))
 
 if(show_replicate){
   p <- p +   geom_quasirandom(size=1,width = 0.2, color='midnightblue', alpha=0.8, groupOnX = F)+facet_wrap(~celltype, scales = 'free_y', ncol = length(celltypes))
   g <- change_strip_background(p, type = 'top', n.color = length(celltypes), strip.color = strip.color)
   print(grid.draw(g))
 } else {
   p
 }
}
+19 −12

File changed.

Preview size limit exceeded, changes collapsed.

+14 −3
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ iri.integrated <- Install.example()
```

### 1. Circlize plot to visualize cell clustering and meta data
This circlize plot was inspired by the data visualization in a published paper (Figure1, https://www.nature.com/articles/s41586-021-03775-x) from Linnarsson's lab.

```
###Check and see the meta data info on your Seurat object
colnames(iri.integrated@meta.data)  
@@ -117,16 +119,25 @@ png(filename = 'upset_plot.png', width = 8, height = 4,units = 'in', res = 300)
complex_upset_plot(iri.integrated, celltype = "NewPT2", group = "Group", min_size = 10, logfc=0.5)
dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/upset_plot.png) <br />

### 7. Other ploting functions
### 7. Cell proportion change across groups

```
png(filename =  'cell_fraction.png', width = 8, height = 4,units = 'in', res = 300)
plot_cell_fraction(iri.integrated,  celltypes = c("PTS1" ,   "PTS2"  ,  "PTS3"  ,  "NewPT1" , "NewPT2"), groupby = "Group", show_replicate = T, rep_colname = "orig.ident")
dev.off()
```

![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/cell_fraction.png) <br />

### 8. Other ploting functions
There are other functions for plotting/data processing in plot1cell. 
```
help(package = plot1cell)
```
Many more functions will be added in the future package development. For questions, please raise an issue in this github page or contact <a href="https://humphreyslab.com">TheHumphreysLab</a>. 

### 8. Attributions
### 9. Attributions
This package uses many methods from Seurat (https://github.com/satijalab/seurat) to process the data for ploting. The circlize and heatmap plots were generated by the circlize (https://github.com/jokergoo/circlize) and ComplexHeatmap (https://github.com/jokergoo/ComplexHeatmap) packages. The Upset plot was generated by the ComplexUpset package (https://github.com/krassowski/complex-upset). The violin plot to show multiple genes across groups was inspired by the codes provided here (https://github.com/ycl6/StackedVlnPlot). Most of the graphs were generated by ggplot2 (https://github.com/tidyverse/ggplot2). The package benefits from the following dependencies.

```

data/cell_fraction.png

0 → 100644
+170 KiB
Loading image diff...