1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| f_pca <- function(lc_counts, lc_g){ res <- prcomp(lc_counts) res <- data.frame(res$rotation, g = lc_g) res$CB <- rownames(res) res }
library(ggplot2) library(tidyverse) f_pca_p <- function(lc_pca, x='PC1', y='PC2'){ ggplot(lc_pca,aes(x=!!sym(x),y=!!sym(y),color=g))+ geom_point()+ theme_bw()+ theme(panel.border=element_blank(),panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.line= element_line(colour = "blue")) }
library(umap) f_umap <- function(lc_pca,dims=1:30){ res <- umap(lc_pca[,dims]) res <- data.frame(res$layout) colnames(res) <- c('UMAP1', 'UMAP2') res$CB <- rownames(res) tp <- lc_pca[,c('g','CB')] res <- merge(res, tp, by="CB") res } f_umap_p <- function(lc_umap){ ggplot(lc_umap,aes(x=UMAP1,y=UMAP2,color=g))+ geom_point()+ theme_bw()+ theme(panel.border=element_blank(),panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.line= element_line(colour = "blue")) }
library(ggplot2) library(reshape2) library(plyr) library(dplyr) library(patchwork) library(purrr) f_gl_boxp <- function(lc_exp, lc_g, lc_delta_ylim){ lc_exp_L = melt(lc_exp) colnames(lc_exp_L) = c('symbol','sample','value') lc_exp_L$group = lc_g p=ggplot(lc_exp_L,aes(x=sample,y=value,fill=group))+geom_boxplot(outlier.shape = NA) p=p+theme_set(theme_set(theme_bw(base_size=20))) p=p+theme(text=element_text(face='bold'),axis.text.x=element_text(angle=30,hjust=1),axis.title=element_blank()) lc_ylim = unlist(by(lc_exp_L, lc_exp_L$sample, function(x){boxplot.stats(x$value)$stats[c(1, 5)]})) lc_ylim <- c(min(lc_ylim), max(lc_ylim)) + lc_delta_ylim p=p + coord_cartesian(ylim = lc_ylim) p } f_icg_boxp <- function(lc_exp, lc_icg, lc_g){ lc_exp_L = melt(lc_exp[lc_icg,]) lc_exp_L <- cbind(lc_exp_L, rownames(lc_exp_L)) colnames(lc_exp_L)=c('value','sample') lc_exp_L$group = lc_g p=ggplot(lc_exp_L,aes(x=group,y=value,fill=group))+geom_boxplot() p=p+stat_summary(fun="mean",geom="point",shape=23,size=3,fill="red") p=p+theme_set(theme_set(theme_bw(base_size=20))) p=p+theme(text=element_text(face='bold'),axis.text.x=element_text(angle=30,hjust=1),axis.title=element_blank()) p=p+ggtitle(lc_icg)+theme(plot.title = element_text(hjust = 0.5)) p }
# 若缺少sva包,则安装 # options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor") # BiocManager::install("sva", update=F) library(sva) library(limma) f_removeBE <- function(lc_dat, lc_batch, lc_g, use_ComBat=F){ if(use_ComBat){ lc_mod <- model.matrix(~as.factor(lc_g)) res <- ComBat(dat = lc_dat, batch = as.factor(lc_batch), mod = lc_mod) }else{ lc_mod <- model.matrix(~0+as.factor(lc_g)) res <- removeBatchEffect(lc_dat, batch = as.factor(lc_batch), design = lc_mod) } res }
library(ggdendro) f_DEG_hclust <- function(lc_counts){ ggdendrogram(hclust(dist(t(lc_counts))), rotate = T, size = 3)+theme(axis.text = element_text(size=14,face = "bold")) }
|