绘制
# 导入数据 mRNA<-read.csv("All_mRNA_FPKM.csv",header=T,row.names=1) exp<-log2(mRNA+1) bar_mat<-t(exp) anno<-read.csv("sample_index.csv",header=T,row.names=1) anno$type2<-anno$Type anno <- anno[rownames(bar_mat),] bar_mat<-bar_mat[rownames(anno),] bar_mat<-as.data.frame(bar_mat) bar_mat$sam=anno$Type # 这里将"Mild","Severe","Critical" 三组合并为 symptomatic组 # 并对数据进行复制并合并 library(tidyverse) plot_data <- data.frame(FOXO3 = bar_mat$FOXO3, group = bar_mat$sam) plot_data2 <- plot_data %>% filter(group != 'Asymptomatic') plot_data2$group = 'Symptomatic' plot_data = rbind(plot_data,plot_data2) head(plot_data) # 设置分组因子水平 plot_data$group<-factor(plot_data$group, levels=c("Symptomatic","Asymptomatic","Mild","Severe","Critical")) library(RColorBrewer) library(ggpubr) library(ggplot2) color <-c("#f06c61","#6b7eb9","#f06c61","#f06c61","#f06c61") # 自行设置差异比较分组 my_comparisons <- list(c("Symptomatic","Asymptomatic")) range(bar_mat$FOXO3) p <- ggplot(plot_data,aes(x = group, y = FOXO3, color = group)) + geom_rect(xmin = 0.4, xmax = 2.5, ymin = -Inf, ymax = Inf, fill ='#d2dbdf', inherit.aes = F)+ geom_jitter(alpha = 0.6)+ geom_boxplot(alpha = 0.5) + scale_color_manual(values = color)+ # 先算一下显著性差异,再手动添加 #geom_signif(comparisons = my_comparisons, # test = "t.test", # map_signif_level = T)+ annotate("text", x = 1.5, y = 10.5, label ="***",size = 4)+ theme_bw() + xlab("") + ylab("Gene Expression (log2 TPM)")+ theme(panel.grid=element_blank(), legend.position = "none", axis.text.x = element_text(angle=90, hjust=1, vjust=.5)) p1 <- p + coord_cartesian(clip = 'off',ylim = c(4,10.6))+ #在非图形区域绘图,且要定好y轴范围 theme(plot.margin = margin(0.5,0.5,1.5,0.5,'cm'))+ #自定义图片上左下右的边框宽度 annotate('segment',x=3,xend=5,y=2.8,yend=2.8,color='black',cex=.4)+ annotate('text',x=4,y=2.7,label='subtype',size=4,color='black') p1 ggsave("p1.pdf",p1,width = 2.5,height = 6)
结果