ggplot做分组柱状图-箱线图-小提琴图添加趋势线

2024-09-12  本文已影响0人  KS科研分享与服务

起因是有小伙伴咨询,做柱状图的时候,怎么添加趋势线,其实很简单,添加一个线性拟合图层即可。刚好最近看到这种图,在做分组表达量箱线图或者小提琴图的时候,在上面添加一个趋势的拟合,可以很清楚的知道表达趋势


image.png

首先读入数据;这里我们选择了多个数据,利用循环作图。

setwd('D:\\KS项目\\公众号文章\\ggplot作图添加趋势线')

library(RColorBrewer)
library(ggpubr)
library(ggplot2)
library(cowplot)

Exp <- read.csv("Exp.csv",header=T,row.names=1)
gene <- c("CD28","CD3D","CD8A","LCK",
          "GATA3","EOMES","IL23A","CXCL8",
          "IL1R2","IL1R1","MMP8","MMP9")
Exp <- log2(Exp+1) 
Exp_plot <- Exp[,gene]


info <- read.csv("info.csv",header=T)
Exp_plot<- Exp_plot[info$Sample,]
Exp_plot$sam=info$Type
Exp_plot$sam <- factor(Exp_plot$sam,
                       levels=c("Asymptomatic",
                                "Mild",
                                "Severe",
                                "Critical"))


col <-c("#5CB85C","#337AB7","#F0AD4E","#D9534F")

小提琴图:


plist2<-list()
for (i in 1:length(gene)) {

  df<-Exp_plot[,c(gene[i],"sam")]
  colnames(df)<-c("Expression","sam")
  my_comparisons1 <- list(c("Asymptomatic", "Mild")) 
  my_comparisons2 <- list(c("Asymptomatic", "Severe"))
  my_comparisons3 <- list(c("Asymptomatic", "Critical"))
  my_comparisons4 <- list(c("Mild", "Severe"))
  my_comparisons5 <- list(c("Mild", "Critical"))
  my_comparisons6 <- list(c("Severe", "Critical"))


  p = ggplot(df, 
         aes(x=sam, y=Expression)) + 
    geom_point(color='#bbbdbf', position = 'jitter') + 
    geom_violin(notch = F, outlier.colour = NA, 
                mapping = aes(fill=as.factor(sam)))+
    geom_smooth(data = df, 
                mapping = aes(x=as.numeric(sam), y=Expression), 
                color='red', se = F, method = 'lm')+
    scale_fill_manual(values = col)+
    theme(axis.line=element_line(colour="black"),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.x = element_text(size = 15,angle = 45,vjust = 1,hjust = 1),
          axis.text.y = element_text(size = 15),
          plot.title = element_text(hjust = 0.5,size=15,face="bold"),
          legend.position = "NA")+
    ggtitle(gene[i])+
    stat_compare_means(method="t.test",hide.ns = F,
                       comparisons =c(my_comparisons1,my_comparisons2,my_comparisons3,my_comparisons4,my_comparisons5,my_comparisons6),
                       label="p.signif")

  plist2[[i]]<-p

}

#cowplot拼图。
plot_grid(plist2[[1]],plist2[[2]],plist2[[3]],
           plist2[[4]],plist2[[5]],plist2[[6]],
           plist2[[7]],plist2[[8]],plist2[[9]],
           plist2[[10]],plist2[[11]],plist2[[12]],ncol=4)
image.png

小提琴图+箱线图:


plist3<-list()

for (i in 1:length(gene)) {

  df<-Exp_plot[,c(gene[i],"sam")]
  colnames(df)<-c("Expression","sam")
  my_comparisons1 <- list(c("Asymptomatic", "Mild")) 
  my_comparisons2 <- list(c("Asymptomatic", "Severe"))
  my_comparisons3 <- list(c("Asymptomatic", "Critical"))
  my_comparisons4 <- list(c("Mild", "Severe"))
  my_comparisons5 <- list(c("Mild", "Critical"))
  my_comparisons6 <- list(c("Severe", "Critical"))


  p = ggplot(df, aes(x=sam, y=Expression)) + 
    geom_point(color='#bbbdbf', position = 'jitter') +
    geom_violin(notch = F, outlier.colour = NA, 
                mapping = aes(fill=as.factor(sam))) +
    geom_boxplot(mapping = aes(fill=as.factor(sam)), width=0.2)+
    geom_smooth(data = df, 
                mapping = aes(x=as.numeric(sam), y=Expression), 
                color='red', se = F, method = 'lm')+
    scale_fill_manual(values = col)+
    theme(axis.line=element_line(colour="black"),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.x = element_text(size = 15,angle = 45,vjust = 1,hjust = 1),
          axis.text.y = element_text(size = 15),
          plot.title = element_text(hjust = 0.5,size=15,face="bold"),
          legend.position = "NA")+
    ggtitle(gene[i])+
    stat_compare_means(method="t.test",hide.ns = F,
                       comparisons =c(my_comparisons1,my_comparisons2,my_comparisons3,my_comparisons4,my_comparisons5,my_comparisons6),
                       label="p.signif")

  plist3[[i]]<-p

}


plot_grid(plist3[[1]],plist3[[2]],plist3[[3]],
          plist3[[4]],plist3[[5]],plist3[[6]],
          plist3[[7]],plist3[[8]],plist3[[9]],
          plist3[[10]],plist3[[11]],plist3[[12]],ncol=4)
image.png

小提琴图+箱线图+曲线拟合:

plist4<-list()

for (i in 1:length(gene)) {

  df<-Exp_plot[,c(gene[i],"sam")]
  colnames(df)<-c("Expression","sam")
  my_comparisons1 <- list(c("Asymptomatic", "Mild")) 
  my_comparisons2 <- list(c("Asymptomatic", "Severe"))
  my_comparisons3 <- list(c("Asymptomatic", "Critical"))
  my_comparisons4 <- list(c("Mild", "Severe"))
  my_comparisons5 <- list(c("Mild", "Critical"))
  my_comparisons6 <- list(c("Severe", "Critical"))


  p = ggplot(df, aes(x=sam, y=Expression)) + 
    geom_point(color='#bbbdbf', position = 'jitter') +
    geom_violin(notch = F, outlier.colour = NA, 
                mapping = aes(fill=as.factor(sam))) +
    geom_boxplot(mapping = aes(fill=as.factor(sam)), width=0.2)+
    geom_smooth(data = df, 
                mapping = aes(x=as.numeric(sam), y=Expression), 
                color='red', se = F, method = 'loess')+ 
    scale_fill_manual(values = col)+
    theme(axis.line=element_line(colour="black"),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.x = element_text(size = 15,angle = 45,vjust = 1,hjust = 1),
          axis.text.y = element_text(size = 15),
          plot.title = element_text(hjust = 0.5,size=15,face="bold"),
          legend.position = "NA")+
    ggtitle(gene[i])+
    stat_compare_means(method="t.test",hide.ns = F,
                       comparisons =c(my_comparisons1,my_comparisons2,my_comparisons3,my_comparisons4,my_comparisons5,my_comparisons6),
                       label="p.signif")

  plist4[[i]]<-p

}


plot_grid(plist4[[1]],plist4[[2]],plist4[[3]],
          plist4[[4]],plist4[[5]],plist4[[6]],
          plist4[[7]],plist4[[8]],plist4[[9]],
          plist4[[10]],plist4[[11]],plist4[[12]],ncol=4)
image.png

柱状图+散点图+拟合:


plist5<-list()

for (i in 1:length(gene)) {
  
  df<-Exp_plot[,c(gene[i],"sam")]
  colnames(df)<-c("Expression","sam")
  my_comparisons1 <- list(c("Asymptomatic", "Mild"))
  my_comparisons2 <- list(c("Asymptomatic", "Severe"))
  my_comparisons3 <- list(c("Asymptomatic", "Critical"))
  my_comparisons4 <- list(c("Mild", "Severe"))
  my_comparisons5 <- list(c("Mild", "Critical"))
  my_comparisons6 <- list(c("Severe", "Critical"))
  
  
  p = ggplot(df, aes(x=sam, y=Expression)) + 
    geom_bar(width = 0.5, aes(fill=sam), stat = 'summary')+ 
    geom_point(color='#bbbdbf', position = 'jitter') +
    geom_smooth(data = df, 
                mapping = aes(x=as.numeric(sam), y=Expression), 
                color='red', se = F, method = 'lm')+
    scale_fill_manual(values = col)+
    theme(axis.line=element_line(colour="black"),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.x = element_text(size = 15,angle = 45,vjust = 1,hjust = 1),
          axis.text.y = element_text(size = 15),
          plot.title = element_text(hjust = 0.5,size=15,face="bold"),
          legend.position = "NA")+
    ggtitle(gene[i])+
    stat_compare_means(method="t.test",hide.ns = F,
                       comparisons =c(my_comparisons1,my_comparisons2,my_comparisons3,my_comparisons4,my_comparisons5,my_comparisons6),
                       label="p.signif")
  
  plist5[[i]]<-p
  
}


plot_grid(plist5[[1]],plist5[[2]],plist5[[3]],
          plist5[[4]],plist5[[5]],plist5[[6]],
          plist5[[7]],plist5[[8]],plist5[[9]],
          plist5[[10]],plist5[[11]],plist5[[12]],ncol=4)
image.png

希望分享对你有用!点着赞再走呗!

上一篇下一篇

猜你喜欢

热点阅读