R语言学习

R语言可视化(十二):MA散点图绘制

2020-08-28  本文已影响0人  Davey1220

12. MA散点图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/12MAplot/")

读取示例数据

data <- read.table("demo_maplot.txt",header = T,
                   check.names = F,row.names = 1,sep="\t")
head(data)
##                 R0_count R3_count R0_fpkm R3_fpkm     log2FC       Pvalue
## OS01T0100100-01      234      199   2.900   2.660 -0.1246267 0.3657893570
## OS01T0100200-01       21       31   0.800   1.280  0.6780719 0.1268392510
## OS01T0100300-00        1        0   0.060   0.001 -5.9068906 1.0000000000
## OS01T0100400-01       56      123   0.980   2.310  1.2370392 0.0000000393
## OS01T0100466-00        0        2   0.001   0.090  6.4918531 0.5000249990
## OS01T0100500-01      323      412   5.460   7.500  0.4579896 0.0000360000
##                         FDR significant
## OS01T0100100-01 0.524535856          no
## OS01T0100200-01 0.222003795          no
## OS01T0100300-00 1.000000000          no
## OS01T0100400-01 0.000000225          up
## OS01T0100466-00 0.653216704          no
## OS01T0100500-01 0.000144630          no

base plot函数绘制MA散点图

attach(data)

# 基础MAplot
plot(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2FC)
image.png
# 设置点的形状,颜色,坐标轴标题
plot(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2FC,
     pch=20,col=ifelse(significant=="up","red",
                       ifelse(significant=="down","green","gray")),
     main="MAplot of R3-vs-R0",
     xlab = "Log2 mean expression",ylab = "Log2 fold change")
image.png
# 添加水平线和图例
abline(h = 0,lty=1,lwd = 2,col="blue")
abline(h = c(-1,1),lty=2,lwd = 2,col="black")
# 添加图例
legend("topright", inset = 0.01, title = "Significant", c("up","no","down"), 
       pch=c(16,16,16),col = c("red","gray","green"))
detach(data)
image.png

ggplot2包绘制MA散点图

library(ggplot2)
head(data)
##                 R0_count R3_count R0_fpkm R3_fpkm     log2FC       Pvalue
## OS01T0100100-01      234      199   2.900   2.660 -0.1246267 0.3657893570
## OS01T0100200-01       21       31   0.800   1.280  0.6780719 0.1268392510
## OS01T0100300-00        1        0   0.060   0.001 -5.9068906 1.0000000000
## OS01T0100400-01       56      123   0.980   2.310  1.2370392 0.0000000393
## OS01T0100466-00        0        2   0.001   0.090  6.4918531 0.5000249990
## OS01T0100500-01      323      412   5.460   7.500  0.4579896 0.0000360000
##                         FDR significant
## OS01T0100100-01 0.524535856          no
## OS01T0100200-01 0.222003795          no
## OS01T0100300-00 1.000000000          no
## OS01T0100400-01 0.000000225          up
## OS01T0100466-00 0.653216704          no
## OS01T0100500-01 0.000144630          no

# 基础MAplot
ggplot(data,aes(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2FC)) + geom_point()
image.png
# 添加点的颜色,坐标轴标题
ggplot(data,aes(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2FC,color=significant)) + 
  geom_point() + theme_bw() +
  labs(title="MAplot of R3-vs-R0",x="Log2 mean expression", y="Log2 fold change")
image.png
# 更改颜色,主题,添加水平线和垂直线,去掉网格线
p <- ggplot(data,aes(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2FC,color=significant)) + 
  geom_point() + theme_bw() +
  labs(title="MAplot of R3-vs-R0",x="Log2 mean expression", y="Log2 fold change") +
  scale_color_manual(values = c("green","gray","red")) +
  geom_hline(yintercept=0, linetype=1, colour="black") +
  geom_hline(yintercept=c(-1,1), linetype=2, colour="gray30") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank())
p
image.png
# 添加基因注释信息
library(ggrepel)
gene_selected <- c("OS12T0196300-01","OS01T0239150-00","OS01T0621400-01",
                   "OS02T0577600-00","OS11T0676100-00","OS12T0613150-00",
                   "OS01T0225500-00","OS02T0101900-01")
data_selected <- data[gene_selected,]
head(data_selected)
##                 R0_count R3_count R0_fpkm R3_fpkm    log2FC    Pvalue
## OS12T0196300-01        3        0   0.070   0.001 -6.129283 0.2500375
## OS01T0239150-00        1        0   0.060   0.001 -5.906891 1.0000000
## OS01T0621400-01        3        0   0.060   0.001 -5.906891 0.2500375
## OS02T0577600-00        2        0   0.060   0.001 -5.906891 0.5000250
## OS11T0676100-00        0        2   0.001   0.070  6.129283 0.5000250
## OS12T0613150-00        0        3   0.001   0.070  6.129283 0.2500375
##                       FDR significant
## OS12T0196300-01 0.3849834          no
## OS01T0239150-00 1.0000000          no
## OS01T0621400-01 0.3849834          no
## OS02T0577600-00 0.6532167          no
## OS11T0676100-00 0.6532167          no
## OS12T0613150-00 0.3849834          no

p + geom_text_repel(data=data_selected, show.legend = F, color="red",
                    aes(label=rownames(data_selected)))
image.png
p + geom_label_repel(data=data_selected, show.legend = F,color="blue",
                     aes(label=rownames(data_selected)))
image.png

ggpubr包绘制MAplot

library(ggpubr)

# 加载示例数据集
data(diff_express)
head(diff_express)
##                     name     baseMean log2FoldChange         padj
## ENSG00000000003   TSPAN6    0.1184475      0.0000000           NA
## ENSG00000000419     DPM1 1654.4618144      0.6789538 5.280802e-02
## ENSG00000000457    SCYL3  681.0463277      1.5263838 3.915112e-07
## ENSG00000000460 C1orf112  389.7226640      3.8933573 1.180333e-14
## ENSG00000000938      FGR  364.7810090     -2.3554014 1.559228e-04
## ENSG00000000971      CFH    1.1346239      1.2932740 4.491812e-01
##                 detection_call
## ENSG00000000003              0
## ENSG00000000419              1
## ENSG00000000457              1
## ENSG00000000460              1
## ENSG00000000938              1
## ENSG00000000971              0

# 基础MAplot
ggmaplot(diff_express, fdr = 0.05, fc = 2, size = 0.4,
         palette = c("red","green","gray"))
image.png
# 更改点的颜色,添加标题,更改基因注释名,字体,背景主题
ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
         fdr = 0.05, fc = 2, size = 0.6,
         palette = c("#B31B21", "#1465AC", "darkgray"),
         genenames = as.vector(diff_express$name),
         xlab = "M",ylab = "A",
         legend = "top", top = 20,
         font.label = c("bold", 11),
         font.legend = "bold",
         font.main = "bold",
         ggtheme = ggplot2::theme_minimal())
image.png
# 添加基因注释边框,更换top基因筛选标准
ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
         fdr = 0.05, fc = 2, size = 0.4,
         palette = c("#B31B21", "#1465AC", "darkgray"),
         genenames = as.vector(diff_express$name),
         legend = "top", top = 20,
         font.label = c("bold", 11), label.rectangle = TRUE,
         font.legend = "bold", select.top.method = "fc",
         font.main = "bold",
         ggtheme = ggplot2::theme_minimal())
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggpubr_0.2.1  magrittr_1.5  ggrepel_0.8.1 ggplot2_3.2.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5       knitr_1.23       tidyselect_0.2.5 munsell_0.5.0   
##  [5] colorspace_1.4-1 R6_2.4.0         rlang_0.4.7      stringr_1.4.0   
##  [9] dplyr_0.8.3      tools_3.6.0      grid_3.6.0       gtable_0.3.0    
## [13] xfun_0.8         withr_2.1.2      htmltools_0.3.6  yaml_2.2.0      
## [17] lazyeval_0.2.2   digest_0.6.20    assertthat_0.2.1 tibble_2.1.3    
## [21] ggsignif_0.5.0   crayon_1.3.4     purrr_0.3.2      glue_1.3.1      
## [25] evaluate_0.14    rmarkdown_1.13   labeling_0.3     stringi_1.4.3   
## [29] compiler_3.6.0   pillar_1.4.2     scales_1.0.0     pkgconfig_2.0.2
上一篇下一篇

猜你喜欢

热点阅读