R绘制相关性图

library(corrgram)
install.packages("corrgram")
library(corrplot)
M <- cor(mtcars)
col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
                           "cyan", "#007FFF", "blue","#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582",
                           "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE",
                           "#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue"))
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
                           "cyan", "#007FFF", "blue", "#00007F"))
wb <- c("white", "black")
# methold参数设定不同展示方式
corrplot(M) #默认methold="circle"

# hclust.method = "ward.D2"设定聚类方法
corrplot(M, hclust.method = "ward.D2",type="lower")# addrect = 4),tl.pos="d"
corrplot(M, add=TRUE,order = "hclust",method = "number",type="upper",tl.pos="n",cl.pos="n")

M
corr1 <- read.csv("D:/QQ_receive/3152983713/FileRecv/corrdata.csv")
corr<-cor(corr1, method = "pearson")
N<-corr
corrplot(N, hclust.method = "ward.D2",type="lower")# addrect = 4),tl.pos="d"
corrplot(N, add=TRUE,order = "hclust",method = "number",type="upper",tl.pos="n", cl.pos="n")
#corrplot(b,add=TRUE, type="lower", method="number",diag=FALSE,tl.pos="n", cl.pos="n",col=col(10))


res1 <- cor.mtest(newR, conf.level = 0.95) 

corrplot(N, method="ellipse",p.mat = res1$p, sig.level = 0.2,order = "AOE", type = "upper") #, tl.pos = "d"
corrplot(N, add = TRUE, p.mat = res1$p, sig.level = 0.2,type = "lower", method = "number", order = "AOE", 
         diag = FALSE, tl.pos = "n", cl.pos = "n") 

N<- cor(newR)
corrplot.mixed(N, lower = "number", upper = "square", tl.col = "black",lower.col = "black", number.cex = 1)  


#另外的方法
library(ggcor)
install.packages('ggcor')
library(rJava)
library(devtools)
library(usethis)

install.packages('rJava')
install.packages('devtools')
install.packages('usethis')

devtools::install_github("zlabx/ggcor")
devtools::install_github("briatte/ggcor")

devtools::install_github("yemanzhongting/ggcor")

install.packages('fansi')

install.packages('digest')

usethis::create_github_token()


usethis::edit_r_environ()

install.packages('corrplot')
library(corrplot)
library(ggcor)

corr<-cor(corr1)
ggcor(corr, type = "full", cor.test = TRUE) + geom_confbox()

library(psych)
corr.test <- corr.test(corr1) 
as_cor_tbl(corr.test)$p.value


install.packages('vegan')
library(vegan) # 使用vegan包所带的数据集
data(varechem)
data(varespec)

library(dplyr)
library(corrplot)

library(corrplot)
par(omi = c(0.3, 0.3, 0.3, 0.3),
    cex = 1.2,
    family = 'Times New Roman') # windows系统可能需要安装其他字体包
M <- cor(mtcars) #计算相关系数矩阵
corrplot(M, method = "square", type = 'upper')

# 准备数据
set.seed(20190420)
n <- ncol(mtcars)
grp <- c('Group01', 'Group02', 'Group03') # 分组名称
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值进行随机抽样
)

# 这一部分代码是按照原图图例说明处理线条宽度和颜色映射
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)
  )

df

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)

#, xlim = c(-5, 14.5)

corr <- fortify_cor(corr1, type = "upper", show.diag = TRUE,cor.test = TRUE, cluster.type = "all")

corr


mantel <- fortify_mantel(varespec, varechem,
                         spec.select = list(spec01 = 22:25,
                                            spec02 = 1:4,
                                            spec03 = 38:43,
                                            spec04 = 15:20),
                         mantel.fun = "mantel.randtest")
ggcor(corr, xlim = c(-5, 14.5)) + 
  add_link(mantel, diag.label = TRUE) +
  add_diaglab(angle = 45) +
  geom_square() + remove_axis("y")



corrplot(corr, hclust.method = "ward.D2",type="lower")# addrect = 4),tl.pos="d"
corrplot(corr,add=TRUE,order = "hclust",method = "number",type="upper",tl.pos="n",cl.pos="n")

#corrplot(b,add=TRUE, type="lower", method="number",diag=FALSE,tl.pos="n", cl.pos="n",col=col(10))

typeof(mtcars)
mtcars
library(readr)
new <- read_csv("C:/Users/Administrator/OneDrive - whu.edu.cn/自然灾害知识图谱/lianjia/new.csv")
View(new)
new
typeof(new)

C(new$year,new$Num)

N<- cor(newR)
corrplot(N) #默认methold="circle"

# hclust.method = "ward.D2"设定聚类方法
corrplot(N, hclust.method = "ward.D2",type="lower")# addrect = 4),tl.pos="d"
corrplot(N, add=TRUE,order = "hclust",method = "number",type="upper",tl.pos="n", cl.pos="n")
#corrplot(b,add=TRUE, type="lower", method="number",diag=FALSE,tl.pos="n", cl.pos="n",col=col(10))


res1 <- cor.mtest(newR, conf.level = 0.95) 

corrplot(N, method="ellipse",p.mat = res1$p, sig.level = 0.2,order = "AOE", type = "upper") #, tl.pos = "d"
corrplot(N, add = TRUE, p.mat = res1$p, sig.level = 0.2,type = "lower", method = "number", order = "AOE", 
         diag = FALSE, tl.pos = "n", cl.pos = "n") 

N<- cor(newR)
corrplot.mixed(N, lower = "number", upper = "circle", tl.col = "black",lower.col = "black", number.cex = 1)  
#tl.col 修改对角线的颜色,lower.col 修改下三角的颜色,number.cex修改下三角字体大小


library(readr)
fillnan <- read_csv("C:/Users/Administrator/Desktop/链家/fillnan.csv")
View(fillnan)
fillnan<-fillnan[,-1]
fillnan<-fillnan[,-2]
fillnan<-fillnan[,-2]
fillnan
corr1<- cor(fillnan)
corrplot.mixed(corr1, lower = "number", upper = "circle", tl.col = "black",lower.col = "red", number.cex = 1)  

library(readr)
delnan <- read_csv("C:/Users/Administrator/Desktop/链家/delnan.csv")
View(delnan)
delnan<-delnan[,-1]
delnan<-delnan[,-2]
delnan<-delnan[,-2]
delnan
corr2<- cor(delnan)
corrplot.mixed(corr2, lower = "number", upper = "circle", tl.col = "red",lower.col = "black", number.cex = 1)  

library(psych)

names(delnan)[1]<-"cases"
names(delnan)[2]<-"fee"
names(delnan)[6]<-"building"
names(delnan)[7]<-"house"
corr2<- cor(delnan)
corrplot.mixed(corr2, lower = "number", upper = "circle", tl.col = "red",lower.col = "black", number.cex = 1)

res1 <- cor.mtest(delnan, conf.level = .95)
corrplot(corr2,p.mat = res1$p, insig = "label_sig",
         sig.level = c( .01, .05), pch.cex = 1.5, pch.col = "black")

wilcox.test()

corr.test(delnan,use="complete")

corrplot.mixed(corr2, lower = "number", upper = "circle", tl.col = "red",lower.col = "black", number.cex = 1)

typeof(corr.test(delnan,use="complete"))
View(delnan)

library(stargazer)

library(car)
install.packages('car')

scatterplotMatrix

corr.p(delnan)

delnan

with(delnan,smoothScatter(cases,sky))

with(delnan,smoothScatter(cases,year))

library(car)

scatterplotMatrix(delnan)

stargazer(corr.test(delnan,use="complete")[3])
fillnan
fit<-lm(cases ~ fee+green+sky+year+building+house+price,data=delnan)

stargazer(summary(fit))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值