222-如何绘制分组相关性散点图?

刘小泽写于2020.12.14 记录一个简单的绘图问题

前言

今天收到一个请求:绘制下图,看到是两个基因的表达量相关性散点图,每个点是一个样本,另外还加上了样本分组信息(4个分组),并且计算了R和P值

整理的数据如下

首先读取看看

给到我的是整理好的excel数据,于是用rio::import读取最佳

rm(list=ls())
options(stringsAsFactors = F)
library(rio)
library(ggpubr)
library(stringr)

x1=import('test.xlsx')
table(x1$Grade)

注意到这里的存在一个特殊字符,绘图时无法显示,因此我们可以自己替换一下

x1$Grade=str_replace(x1$Grade,"Grade Ⅱ",'Grade-II')
x1$Grade=str_replace(x1$Grade,"Grade Ⅲ",'Grade-III')

然后搜索绘图代码

首先还是想用ggpubr简单一点,因为有大量的现成代码:http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/78-perfect-scatter-plots-with-correlation-and-marginal-histograms/

先做个分组散点图吧

ggscatter(x1, x = "CBX6", y = "TWIST1",
          conf.int = TRUE,                         
          color = "Grade", palette = "jco")         

然后想添加趋势线

ggscatter(x1, x = "CBX6", y = "TWIST1",
          add = "reg.line",
          conf.int = TRUE,                         
          color = "Grade", palette = "jco"         
) +stat_cor(method = "pearson")   

不过这不是我想要的,我只想画一条趋势线而已

还是ggplot2最强大

解决问题最重要,不去纠结具体ggpubr的参数设置了

ggplot(x1, aes(x = CBX6, y = TWIST1)) +
  geom_point(aes(colour = Grade)) +
  geom_smooth(method = "lm")+
  stat_cor(method = "pearson")+theme_classic()

最后,想要分面显示也可以

ggplot(x1, aes(x = CBX6, y = TWIST1)) +
  geom_point(aes(colour = Grade)) +
  geom_smooth(method = "lm")+
  stat_cor(method = "pearson")+theme_classic()+
  facet_wrap(~Grade) 

Yunze Liu
Yunze Liu
Bioinformatics Sharer

Co-founder of Bioinfoplanet(生信星球)

Next
Previous

Related