相关性气泡图-数据模拟到作图(自备)

目录

普通气泡图ggplot2

相关性气泡图

数据处理1:生成两个dataframe

数据处理2:计算相关性R和P

数据处理3:添加细节

绘图

核心:气泡的信息主要体现在气泡大小和气泡颜色变化。

普通气泡图ggplot2
复制代码
rm(list = ls()) 
library(ggrepel)
library(ggplot2)
dat <- mtcars

数据格式查看:

复制代码
head(dat)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 
复制代码
p1 <- ggplot(data=mtcars, aes(x=wt,y=mpg))+
  geom_point(aes(size=disp,fill=disp),    ##展示点的数据
             shape=21,colour="black",alpha=0.8)+ 
  # 绘制气泡图,填充颜色和面积大小
  scale_fill_gradient2(low="#377EB8",high="#E41A1C",
                       limits = c(0,max(mtcars$ disp)), 
                       midpoint = mean(mtcars$disp))+ #设置填充颜色映射主题(colormap)
  scale_size_area(max_size=12)+ # 设置显示的气泡图气泡最大面积
  geom_text_repel(label = mtcars$disp)+ # 展示disp的具体数值
  theme_bw()
p1
dev.off()

更多关于标签相关的设置:R语言可视化学习笔记之ggrepel包 - 简书 (jianshu.com)

R语言-ggplot自定义点的形状、线条的类型_ggplot点的形状-CSDN博客


相关性气泡图

气泡大小展示相关系数和气泡颜色都只有一个图例:

数据处理1:生成两个dataframe
复制代码
rm(list = ls()) 
library(ggplot2)
library(tidyverse)
library(ggrepel)
library(corrplot)
library(reshape2)
library(Hmisc)

#生成模拟数据①:表达矩阵
data <- sample(1:15, 225, replace = TRUE)
matrix_data <- matrix(data, nrow = 15, ncol = 15)
df <- data.frame(matrix_data)
colnames(df) <- paste0("gene",1:ncol(df))
rownames(df) <- paste0("sample",1:nrow(df))

#生成模拟数据②:某种评分矩阵
data1 <- sample(50:100, 225,replace = TRUE)
matrix_data1 <- matrix(data1, nrow = 15, ncol = 15)
df1 <- data.frame(matrix_data1)
colnames(df1) <- paste0("score",1:ncol(df))
rownames(df1) <- paste0("sample",1:nrow(df))
复制代码
head(df)
        gene1 gene2 gene3 gene4 gene5 gene6 gene7 gene8 gene9 gene10 gene11
sample1     6     3     9    15     6     9    13    15    15      6     14
sample2     1    15    10    11    12     1    11     9    10      7     11
sample3     7    10     9     5     8     6     3     9     5      9     13
复制代码
head(df1)
        score1 score2 score3 score4 score5 score6 score7 score8 score9
sample1     81     95     70     75     83     88     87     61     54
sample2     61     63     85     69     59     83     55     82     60
sample3     81     56     85     59     67     74     56     74     59

数据处理2:计算相关性R和P
复制代码
##进行相关性R和P值计算
dat <- cbind(df,df1)
dat1 <- as.matrix(dat)##需要为matrix格式
rescor <- rcorr(dat1,type="spearman")#计算相关性 ("pearson","spearman")

##R值数据处理
rescor_R <- rescor$r
rescor_R1 <- as.data.frame(rescor_R[colnames(df),colnames(df1)])##提取相关性R值
rescor_R1$GENE <- rownames(rescor_R1)##转换为长数据
rescor_long <- melt(rescor_R1, id.vars = c("GENE"))
##P值数据处理
rescor_P <- rescor$P
rescor_P1 <- as.data.frame(rescor_P[colnames(df),colnames(df1)])##提取相关性P值
rescor_P1$GENE <- rownames(rescor_P1)##转换为长数据
rescor_longp <- melt(rescor_P1, id.vars = c("GENE"))
colnames(rescor_longp) <- c("GENEP","variableP","valueP")

identical(rescor_long$GENE,rescor_longp$GENE)#[1] TRUE  前两列数据一致,进行合并
identical(rescor_long$variable,rescor_longp$variable)#[1] TRUE
res <- cbind(rescor_long,rescor_longp)
res1 <- res[,c(1:3,6)]
复制代码
head(res1)
   GENE variable        value    valueP
1 gene1   score1  0.069495604 0.8056036
2 gene2   score1  0.084838789 0.7637076
3 gene3   score1 -0.277011560 0.3175398
4 gene4   score1 -0.108011195 0.7016025

数据处理3:添加细节
复制代码
colnames(res1) <- c("GENE","SCORE","correlation","pvalue")
#绘图添加分列#
#添加一列,来判断pvalue值范围
res1$sign<-case_when(res1$pvalue >0.05~">0.05",
                      res1$pvalue <0.05 &res1$pvalue>0.01 ~"<0.05",
                      res1$pvalue <0.01 &res1$pvalue>0.001 ~"<0.01",
                      res1$pvalue <0.001 &res1$pvalue>0.0001~"<0.001",
                      res1$pvalue <0.0001 ~"<0.0001")
#添加一列来判断correlation的正负
res1$core<-case_when(res1$correlation >0 ~"positive",
                      res1$correlation<0 ~"negtive")
复制代码
head(res1)
   GENE  SCORE  correlation    pvalue  sign     core
1 gene1 score1  0.069495604 0.8056036 >0.05 positive
2 gene2 score1  0.084838789 0.7637076 >0.05 positive
3 gene3 score1 -0.277011560 0.3175398 >0.05  negtive
4 gene4 score1 -0.108011195 0.7016025 >0.05  negtive
绘图

#主要是两种填充方式shape=21和shape=16

复制代码
#主要是两种填充方式shape=21和shape=16
p <- ggplot(data=res1,aes(x=GENE,y=SCORE))+
  #正相关的点(正负相关的点分开绘制)
  geom_point(data=filter(res1,core=="positive"),
             aes(x=GENE,y=SCORE,size=abs(correlation),fill=sign),shape=21)+
  #负相关的点
  geom_point(data=filter(res1,core=="negtive"),
             aes(x=GENE,y=SCORE,size=abs(correlation),color=sign),shape=16)+
  #自定义颜色fill为正相关颜色填充
  scale_fill_manual(values=c("#FF6666","#FF9999","#FFCCCC","#FFCCCC","#CCCCCC"))+
  #自定义颜色负相关颜色
  scale_color_manual(values=c("#333366","#336699","#3399CC","#66CCFF","#CCCCCC"))+
  #去除x轴和y轴
  labs(x="",y="")+
  #修改主题
  theme_bw()+
  theme(axis.text.x=element_text(angle=45,hjust=1),
        axis.ticks.x=element_blank())+
  #修改legend,title表示标题,order表示标题的顺序,override.aes来设置大小
  guides(color=guide_legend(title="Negitive",order=1,override.aes=list(size=4)),
         size=guide_legend(title="Spearman's p",order=2),#为相关性大小
         fill=guide_legend(title="Positive",order=3,override.aes=list(size=4)))+#fill为正相关
  labs(title = "Spearman's p Correlation") 
p
dev.off()
相关推荐
困死,根本不会2 小时前
Kivy+Buildozer 打包 APK 踩坑:python-for-android 克隆失败
开发语言·php·kivy
咸鱼2.04 小时前
【java入门到放弃】跨域
java·开发语言
sg_knight5 小时前
设计模式实战:模板方法模式(Template Method)
python·设计模式·模板方法模式
FreakStudio5 小时前
ESP32居然能当 DNS 服务器用?内含NCSI欺骗和DNS劫持实现
python·单片机·嵌入式·面向对象·并行计算·电子diy
skiy5 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
一念春风5 小时前
智能文字识别工具(AI)
开发语言·c#·wpf
乐观勇敢坚强的老彭5 小时前
2026全国青少年信息素养大赛考纲
python·数学建模
桦06 小时前
【C++复习】:继承
开发语言·c++
YMWM_6 小时前
【问题】thor上的cubLas
linux·python·thor