library(vegan)
library(dplyr)
library(corrplot)
library(Hmisc)
library(psych)
library(tidyverse)
mydata <- read.csv(file.choose())
head(mydata)
M <- cor(mydata,method = 'spearman')
class(M)
M[1:5,1:5]
symnum(M)
res1 <- cor.mtest(M,conf.level = 0.95) # ???嵼?????ݵ???????
corrplot(M, method = "circle", type = 'upper', order = "hclust",addrect = 3, p.mat = res1$p, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white")
#corr_matrix <- corr.test(M, method = 'spearman') # ??д????????
#corr_matrix$r
#corr_matrix$p
head(mtcars)
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(df$gx, df$gy, df$x, df$y, lty = 'solid', lwd = df$lwd,
col = df$lcol, 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)
library(pheatmap)
library(vegan)
library(dplyr)
library(corrplot)
library(Hmisc)
library(psych)
library(tidyverse)
df<-read.csv("cluster.csv",header=T)
df[is.na(df)]<-0
df
pheatmap(df)这是什么代码