Commit cc309fa1 authored by HaojiaWu's avatar HaojiaWu
Browse files

Bud fix

parent c8293255
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ export(creat_cellphonedb_file)
export(create_pyscenic_file)
export(data_processing)
export(extract_gene_count)
export(firstup)
export(get_metadata)
export(get_segment)
export(mk_color_table)
+79 −39
Original line number Diff line number Diff line
@@ -24,12 +24,13 @@ complex_vlnplot_single <- function(
  groups,
  add.dot = T,
  font.size=14,
  pt.size=0.1
  pt.size=0.1,
  split.by=NULL
){
  if(is.null(cell.types)){
    cell.types = levels(seu)
  } 
  gene_count<-extract_gene_count(seu=seu, features = feature, cell.types = cell.types, meta.groups = groups)
  gene_count<-extract_gene_count(seu=seu, features = feature, cell.types = cell.types, meta.groups = c(groups, split.by))
  set.seed(seed = 42)
  noise <- rnorm(n = length(x = gene_count[,feature])) / 100000
  gene_count[, feature]<-gene_count[,feature]+noise
@@ -48,13 +49,18 @@ complex_vlnplot_single <- function(
      if(add.dot){
        p = p + geom_quasirandom(size=pt.size, alpha=0.2)
      }
      if(!is.null(split.by)){
        p = p + facet_wrap(as.formula(paste("~", split.by)), scales = 'free_x')
      }
      p
    } else {
      if(is.null(split.by)){
        plot_list<-list()
        for(i in 1:length(cell.types)){
          cell_count <- gene_count[gene_count$celltype==cell.types[i],]
          p<-ggplot(cell_count, aes_string(x=groups, y=feature, fill=groups))+
            geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
            xlab("") + ylab(cell.types[i]) +
            theme(panel.background = element_rect(fill = "white",colour = "black"),
                  legend.position = "none", 
                  axis.text.x = element_blank(), 
@@ -70,9 +76,42 @@ complex_vlnplot_single <- function(
        plot_list[[length(plot_list)]]<- plot_list[[length(plot_list)]] +
          theme(axis.text.x=element_text(angle = 45, hjust = 1, vjust = 1, size = font.size), axis.ticks.x = element_line())
        p<- patchwork::wrap_plots(plotlist = plot_list, ncol = 1)  + patchwork::plot_annotation(title = feature) & theme(plot.title = element_text(hjust = 0.5, size = (font.size +2)))
    }
        p
      } else {
        p<-ggplot(gene_count, aes_string(x = "group", y = feature, fill = "group")) +
          facet_grid(as.formula(paste("celltype","~","group2")), scales = "free_x") +
          geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
          xlab("") +
          ylab(paste(feature,"expression")) +
          theme_bw() +
          theme(panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                strip.text = element_text( size = font.size),
                axis.text.x = element_text(size=(font.size-2), angle = 45, hjust = 1, vjust = 1),
                axis.title.y = element_text(size = font.size),
                legend.position = "none")
        if(add.dot){
          p = p + geom_quasirandom(size=pt.size, alpha=0.2)
        }
        g <- ggplot_gtable(ggplot_build(p))
        strip_t <- which(grepl('strip-t', g$layout$name))
        strip_r <- which(grepl('strip-r', g$layout$name))
        strip_both<-c(strip_t, strip_r)
        ncol <- length(cell.types) + length(names(table(gene_count[,split.by])))
        fills <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(12, "Paired"))(ncol)
        k <- 1
        for (i in strip_both) {
          j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
          g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
          k <- k+1
        }
        print(grid::grid.draw(g))
      }
    }
  } else {
    if(!is.null(split.by)){
      stop("This function does not support spliting multiple groups. Plots will look too messy! Please select one group only in the 'groups' parameter if you want to use 'split.by'.")
    }
    if(length(cell.types)==1){
      gene_count<-melt(gene_count[,c(feature, groups)], measure.vars  = groups)
      p<-ggplot(gene_count, aes_string(x="value", y=feature, fill="value"))+
@@ -82,6 +121,8 @@ complex_vlnplot_single <- function(
              axis.title = element_text(size = font.size), 
              axis.text.x = element_text(size = font.size, angle = 45, hjust = 1, vjust = 1),
              axis.text.y = element_text(size=(font.size-2)),
              strip.background =element_rect(fill="lemonchiffon1"),
              strip.text = element_text( size = font.size),
              legend.title = element_blank(),
              legend.position = 'none',
              plot.title = element_text(size=(font.size+2), hjust = 0.5))+
@@ -124,4 +165,3 @@ complex_vlnplot_single <- function(
    }
  }
}
 No newline at end of file
+17 −0
Original line number Diff line number Diff line
@@ -363,3 +363,20 @@ extract_gene_count <- function(
  feature_count$UMAP2<-umap_data$UMAP_2
  feature_count
}


#' A function to change gene name into first letter capital
#'
#' The function is modified from this thread: https://stackoverflow.com/questions/18509527/first-letter-to-upper-case/18509816
#'
#' @param gene Gene name
#' @export
#' 

firstup <- function(
  gene
  ){
  x <- tolower(gene)
  substr(x, 1, 1) <- toupper(substr(x, 1, 1))
  x
}
+2 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
This package allows users to visualize the single cell data on the R objects or output files generated by the popular tools such as Seurat, Signac, SCENIC, monocle, CellPhoneDB, CellChat etc. It is currently under active development. A vignette will be available soon.

```
# How to install:
# How to install in R:

devtools::install_github("HaojiaWu/plot1cell")
```
+2 −1
Original line number Diff line number Diff line
@@ -11,7 +11,8 @@ complex_vlnplot_single(
  groups,
  add.dot = T,
  font.size = 14,
  pt.size = 0.1
  pt.size = 0.1,
  split.by = NULL
)
}
\arguments{
Loading