美文网首页plot
跟着 Cell 学作图 | 箱线图+散点(组间+组内差异分析)

跟着 Cell 学作图 | 箱线图+散点(组间+组内差异分析)

作者: 木舟笔记 | 来源:发表于2022-06-03 11:35 被阅读0次

    [图片上传失败...(image-3ed7ef-1654227300997)]

    今天我们复现一幅2022年3月发表在Cell上的箱线图

    Title:Tissue-resident FOLR2+ macrophages associate with CD8+ T cell infiltration in human breast cancer

    DOI:https://doi.org/10.1016/j.cell.2022.02.021

    之前复现过的箱线图

    1. 跟着 Cell 学作图 | 3.箱线图+散点+差异显著性检验
    2. 跟着Nat Commun学作图 | 1.批量箱线图+散点+差异分析
    3. 跟着Nat Commun学作图 | 4.配对箱线图+差异分析
    4. R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
    5. 跟着Nature学作图 | 质控箱线图

    22

    [图片上传失败...(image-2301f1-1654227300997)]

    读图

    本期箱线图亮点:

    1. 加上了亚组的比较
    2. 组间和组内背景不同

    结果展示

    [图片上传失败...(image-2ab642-1654227300997)]

    示例数据和代码领取

    详见:https://mp.weixin.qq.com/s/O6Yx-dFac-ycUUNj_bzM8w

    image.png

    绘制

    # 导入数据
    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)
    

    [图片上传失败...(image-1c0938-1654227300997)]

    往期内容

    1. (免费教程+代码领取)|跟着Cell学作图系列合集
    2. Q&A | 如何在论文中画出漂亮的插图?
    3. 跟着 Cell 学作图 | 桑葚图(ggalluvial)
    4. R实战 | Lasso回归模型建立及变量筛选
    5. 跟着 NC 学作图 | 互作网络图进阶(蛋白+富集通路)(Cytoscape)
    6. R实战 | 给聚类加个圈圈(ggunchull)
    7. R实战 | NGS数据时间序列分析(maSigPro)

    [图片上传失败...(image-2ce8dc-1654227300997)]

    相关文章

      网友评论

        本文标题:跟着 Cell 学作图 | 箱线图+散点(组间+组内差异分析)

        本文链接:https://www.haomeiwen.com/subject/pjocmrtx.html