其他分享
首页 > 其他分享> > 基因卡方列联表P值检验——相关系数图形

基因卡方列联表P值检验——相关系数图形

作者:互联网

相关系数图

library(vegan)
library(dplyr)
library(corrplot)
par(omi = c(0.3, 0.3, 0.3, 0.3),
cex = 1.2,
family = ‘Times New Roman’) # windows系统可能需要安装其他字体包
M <- cor(decostand(mtcars,method=“hellinger”,na.rm=T))#计算相关系数矩阵
corrplot(M, method = “circle”, type = ‘upper’)
head(mtcars)
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 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

在这里插入图片描述

加注标签的相关系数图形

在这里插入图片描述

#准备数据
set.seed(20190420)
n <- ncol(mtcars)
grp <- c(‘Cluster_1’, ‘Cluster_2’, ‘Cluster_3’) # 分组名称
sp <- c(rep(0.0008, 6), rep(0.007, 2), rep(0.03, 3), rep(0.13, 22)) # P值
gx <- c(-4.5, -2.5, 1) # 分组的X坐标
gy <- c(n-1, n-5, 2.5) # 分组的Y坐标
df <- data.frame(
grp = rep(grp, each = n), # 分组名称,每个重复n次
gx = rep(gx, each = n), # 组X坐标,每个重复n次
gy = rep(gy, each = n), # 组Y坐标,每个重复n次
x = rep(0:(n - 1) - 0.5, 3), # 变量连接点X坐标
y = rep(n:1, 3), # 变量连接点Y坐标
p = sample(sp), # 对人工生成p值进行随机抽样
r = sample(c(rep(0.8, 4), rep(0.31, 7), rep(0.12, 22)))
#对人工生成r值进行随机抽样
)

length(rep(grp, each = n))
length(rep(gx, each = n))
length(rep(gy, each = n))
length(rep(0:(n - 1) - 0.5, 3))
length(rep(n:1, 3))
length(sample(sp))
length(sample(c(rep(0.8, 4), rep(0.31, 7), rep(0.12, 22))) )

#这一部分代码是按照原图图例说明处理线条宽度和颜色映射
df <- df %>%
mutate(
lcol = ifelse(p <= 0.001, ‘#1B9E77’, NA),
# p值小于0.001时,颜色为绿色,下面依次类推
lcol = ifelse(p > 0.001 & p <= 0.01, ‘#88419D’, lcol),
lcol = ifelse(p > 0.01 & p <= 0.05, ‘#A6D854’, lcol),
lcol = ifelse(p > 0.05, ‘#B3B3B3’, lcol),
lwd = ifelse(r >= 0.5, 14, NA),
# r >= 0.5 时,线性宽度为14,下面依次类推
lwd = ifelse(r >= 0.25 & r < 0.5, 7, lwd),
lwd = ifelse(r < 0.25, 1, lwd)
)

#核心函数:segments。

segments(df g x , d f gx, df gx,dfgy, df x , d f x, df x,dfy, lty = ‘solid’, lwd = df l w d , c o l = d f lwd, col = df lwd,col=dflcol, xpd = TRUE) # 绘制连接线

points(gx, gy, pch = 24, col = ‘blue’, bg = ‘blue’, cex = 3, xpd = TRUE)
#组标记点
text(gx - 0.5, gy, labels = grp, adj = c(1, 0.5), cex = 1.5, xpd = TRUE)
#组名称

labels01 <- c(’<= 0.001’,‘0.001 < x <= 0.01’,‘0.01 < x <= 0.05’,’> 0.05’)
labels02 <- c(’>= 0.5’, ‘0.25 - 0.5’, ‘< 0.25’)
labels_x <- rep(-6, 4)
labels_y <- seq(4.6, 2.6, length.out = 4)
text(-6.5, 5.2, ‘P-value’, adj = c(0, 0.5), cex = 1.2, font = 2, xpd = TRUE)
text(labels_x, labels_y, labels01, adj = c(0, 0.5), cex = 1.2, xpd = TRUE)
points(labels_x - 0.5, labels_y, pch = 20, col = c(’#1B9E77’, ‘#88419D’,’#A6D854’, ‘#B3B3B3’),
cex = 3, xpd = TRUE)
lines_x <- c(-6.5, -3, 0.5)
lines_y <- rep(1.2, 3)
text(-6.5, 1.9, “Mantel’s r”, adj = c(0, 0.5), cex = 1.2, font = 2, xpd = TRUE)
text(lines_x + 1.5, lines_y, labels02, adj = c(0, 0.5), cex = 1.2, xpd = TRUE)
segments(lines_x, lines_y, lines_x + 1, lines_y, lwd = c(14, 7, 2.5), lty = ‘solid’,
col = ‘#B3B3B3’, xpd = TRUE)
##图例框框
segments(-6.9, 5.6, -2.8, 5.6, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)
segments(-2.8, 5.6, -2.8, 1.8, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)
segments(-2.8, 1.8, 3.6, 1.8, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)
segments(3.6, 1.8, 3.6, 0.7, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)
segments(3.6, 0.7, -6.9, 0.7, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)
segments(-6.9, 0.7, -6.9, 5.6, lty = ‘solid’, lwd = 1.2,
col = ‘grey50’, xpd = TRUE)

相关系数加标签

在这里插入图片描述
install.packages(“ggpubr”)
library(ggpubr)
my_data <- mtcars
cor(my_data d r a t , m y d a t a drat,my_data drat,myd​atampg)
ggscatter(my_data,
x = “drat”, #x变量
y = “mpg”,#y变量
add = “reg.line”,##拟合曲线
conf.int = TRUE,##置信区间阴影带
cor.coef = TRUE, ##系数
cor.method = “pearson”,#方法
xlab = “drat”, ## x轴
ylab = “mg”)## y轴

标签:相关系数,0.5,xpd,col,联表,卡方列,TRUE,rep,lwd
来源: https://blog.51cto.com/u_15264819/2887648