liyue 2021/6/30

    获取更多R语言知识,请关注公众号:医学和生信笔记

    医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!

    本次画图使用的数据来自于CIBERSOFT和ssGSEA的结果,需要自己根据给出的格式整理。

    画个图

    1. rm(list = ls())
    2. library(tidyverse)
    3. ## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
    4. ## v ggplot2 3.3.3 v purrr 0.3.4
    5. ## v tibble 3.1.1 v dplyr 1.0.6
    6. ## v tidyr 1.1.3 v stringr 1.4.0
    7. ## v readr 1.4.0 v forcats 0.5.1
    8. ## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
    9. ## x dplyr::filter() masks stats::filter()
    10. ## x dplyr::lag() masks stats::lag()
    11. load(file = "./rdata/coadread_cibersoft.RData")
    12. res_cibersort1 <- as.data.frame(res_cibersort,stringsAsFactors = F)
    13. knitr::kable(head(res_cibersort1))
    B cells naive B cells memory Plasma cells T cells CD8 T cells CD4 naive T cells CD4 memory resting T cells CD4 memory activated T cells follicular helper T cells regulatory (Tregs) T cells gamma delta NK cells resting NK cells activated Monocytes Macrophages M0 Macrophages M1 Macrophages M2 Dendritic cells resting Dendritic cells activated Mast cells resting Mast cells activated Eosinophils Neutrophils P-value Correlation RMSE
    TCGA-AA-3979-01A-01R-1022-07 0.0054392 0 0.0929004 0.0000000 0 0.1345505 0.1951179 0.0010103 0.0059308 0.0000000 0.0049664 0.0000000 0.0000000 0.2164667 0.0475687 0.1686659 0.0003992 0.0043228 0.0000000 0.0305069 0.0000000 0.0921542 0.4 0.0403932 1.073114
    TCGA-AG-A032-01A-01R-A00A-07 0.0896438 0 0.0584290 0.0225665 0 0.2393689 0.0000000 0.0785031 0.1000758 0.0000000 0.0000000 0.0459226 0.0000000 0.1619750 0.0492802 0.0771410 0.0356558 0.0118161 0.0296222 0.0000000 0.0000000 0.0000000 0.3 0.0451298 1.068029
    TCGA-AA-3655-01A-02R-1723-07 0.0408891 0 0.0766110 0.0228790 0 0.1981505 0.0000000 0.0000000 0.1043474 0.0000000 0.0000543 0.0000000 0.0000000 0.2476955 0.0859603 0.1745513 0.0064078 0.0114325 0.0215842 0.0094370 0.0000000 0.0000000 0.1 0.1487438 1.034526
    TCGA-AG-3894-01A-01R-1119-07 0.0432472 0 0.0840875 0.0866711 0 0.1483661 0.0396667 0.0051174 0.0040562 0.0000000 0.0328460 0.0000000 0.0019404 0.1309107 0.0542465 0.2809492 0.0000000 0.0000000 0.0493978 0.0000000 0.0384972 0.0000000 0.1 0.0846817 1.047211
    TCGA-D5-6535-01A-11R-1723-07 0.0411714 0 0.0791348 0.0065910 0 0.1877118 0.0104629 0.0620132 0.0511255 0.0016361 0.0245012 0.0000000 0.0000000 0.2825045 0.0968499 0.1228615 0.0000000 0.0000000 0.0000000 0.0334362 0.0000000 0.0000000 0.1 0.1516030 1.035827
    TCGA-CK-4950-01A-01R-1723-07 0.0254992 0 0.1997683 0.0966707 0 0.0985558 0.0393477 0.0518356 0.0388726 0.0000000 0.0000000 0.0000000 0.0000000 0.1693512 0.0402702 0.1418299 0.0088852 0.0000000 0.0000000 0.0675871 0.0022089 0.0193177 0.1 0.1037796 1.033874
    1. #添加分组信息
    2. coread_cibersoft <- res_cibersort1 %>%
    3. rownames_to_column(var = "sample_id") %>%
    4. mutate(sampletype = ifelse(as.numeric(as.character(str_sub(sample_id,14,15)))<10,
    5. "tumor","normal")) %>%
    6. select("P-value",Correlation,RMSE,sampletype,everything()) %>%
    7. pivot_longer(cols = 6:27,names_to = "celltype",values_to = "proportion")
    8. #save(coad_cibersoft,file = "coad_cibersoft.RData")
    9. library(ggplot2)
    10. library(ggpubr)
    11. library(ggsci)
    12. #直方图
    13. dd1 <- coread_cibersoft
    14. library(RColorBrewer)
    15. mypalette <- colorRampPalette(brewer.pal(8,"Set1"))
    16. ggplot(data =dd1, aes(x = sample_id, y = proportion,fill = celltype))+
    17. geom_bar(stat = "identity",position = "stack")+
    18. labs(fill = "Cell Type",x = "",y = "Estiamted Proportion") +
    19. theme_bw() +
    20. theme(axis.text.x = element_blank(),
    21. axis.ticks.x = element_blank(),
    22. legend.position = "bottom") +
    23. scale_y_continuous(expand = c(0.01,0)) +
    24. scale_fill_manual(values = mypalette(22))

    image.png

    1. # 有顺序的箱线图
    2. library(forcats)
    3. ggplot(dd1,aes(fct_reorder(celltype, proportion),proportion,fill = celltype)) +
    4. geom_boxplot(outlier.shape = 21,color = "black") +
    5. theme_bw() +
    6. labs(x = "Cell Type", y = "Estimated Proportion") +
    7. theme(axis.text.x = element_blank(),
    8. axis.ticks.x = element_blank(),
    9. legend.position = "bottom") +
    10. scale_fill_manual(values = mypalette(22))

    image.png
    分组箱线图

    1. dd1$Group = ifelse(as.numeric(str_sub(dd1$sample_id,14,15))<10,"tumor","normal")
    2. library(ggpubr)
    3. ggplot(dd1,aes(fct_reorder(celltype,proportion),proportion,fill = Group)) +
    4. geom_boxplot(outlier.shape = 21,color = "black") +
    5. theme_bw() +
    6. labs(x = "Cell Type", y = "Estimated Proportion") +
    7. theme(legend.position = "top") +
    8. theme(axis.text.x = element_text(angle=80,vjust = 0.5))+
    9. scale_fill_manual(values = mypalette(22)[c(6,1)])+
    10. stat_compare_means(aes(group = Group,label = ..p.signif..),method = "kruskal.test")

    image.png
    热图

    1. library(pheatmap)
    2. k <- apply(res_cibersort1[,-c(23:25)],2,function(x) {sum(x == 0) < nrow(res_cibersort1)/2})
    3. table(k)
    1. ## k
    2. ## FALSE TRUE
    3. ## 7 15
    1. re2 <- as.data.frame(t(res_cibersort1[,-c(23:25)][,k]))
    2. Group <- ifelse(as.numeric(as.character(str_sub(rownames(res_cibersort1),14,15)))<10, "tumor","normal")
    3. an <- data.frame(group = Group,
    4. row.names = rownames(res_cibersort1))
    5. pheatmap(re2,scale = "row",
    6. show_colnames = F,
    7. annotation_col = an,
    8. color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

    image.png

    1. ### 画图
    2. rm(list = ls())
    3. load(file = "./rdata/gsva_data_TCGA_COADREAD.Rdata")
    4. coread_gsva <- as.data.frame(t(gsva_data))
    5. test <- coread_gsva[1:10,1:10]
    6. # 添加分组信息
    7. library(tidyverse)
    8. coread_gsva <- coread_gsva %>%
    9. rownames_to_column(var = "sample_id") %>%
    10. mutate(sampletype = ifelse(as.numeric(as.character(str_sub(sample_id,14,15)))<10,
    11. "tumor","normal")) %>%
    12. select(sampletype,everything())
    13. #画图
    14. dd1 <- coread_gsva %>%
    15. pivot_longer(cols=3:30,
    16. names_to= "celltype",
    17. values_to = "NES")
    18. library(ggplot2)
    19. library(ggpubr)
    20. library(ggsci)
    21. #箱线图,还可以画小提琴图等
    22. ggplot(data =dd1, aes(x = celltype, y = NES))+
    23. geom_boxplot(aes(color = sampletype),outlier.shape = NA)+
    24. scale_color_lancet()+
    25. theme_bw()+
    26. theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = "black"))+
    27. stat_compare_means(aes(group=sampletype), label = "p.signif")

    image.png

    获取更多R语言知识,请关注公众号:医学和生信笔记

    医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!