devtools::install_github("Hy4m/linkET", force = TRUE)
library(devtools)
library(usethis)
library(cols4all)
library(writexl)
library(leaps)
library(tidyverse)
library(cowplot)
library(colorspace)
library(ggrepel)
library(car)
library(reshape2)
library(readxl)
library(ggplot2)
library(cols4all)
library(GGally)
# 如果需要安装缺少的包,可以运行以下语句安装
install.packages("ggplot2")
install.packages("ggsignif")
install.packages("gghalves")
# 加载已安装的包
library(ggplot2)
library(ggsignif)
library(gghalves)
setwd("G:/LW_DATE/YEB_CS/data")
CSTJ=read_excel("CS分段统计.xlsx",sheet = "CSTJ3")#CSTG0010
cor <- correlate(
cbind(CSTJ$CS2000,CSTJ[34:47]),
method = "pearson", #pearson", "kendall" or "spearman"
adjust = TRUE,
adjust_method = "fdr"
)
#复杂热图
# install.packages("GGally")
library(GGally)
colu2000 <- cbind(CSTJ$CS2000, CSTJ[39:47], CSTJ[13])
colu2000 <-na.omit(colu2000)
colu2000 <-as.numeric(colu2000)
colu2000 <- as.data.frame(lapply(colu2000, as.numeric))
ggpairs(colu2000, # Data frame
columns = 1:10, # Columns
ggplot2::aes(color = colu2000[[11]]), # 按分组信息分别着色、计算相关性系数和拟合
upper = list(continuous = "cor"),
lower = list(continuous = wrap("smooth",method = "loess",se = FALSE)))+ # se = FALSE,不展示置信区间
#diag = list(continuous = my_density))+
# ggplot2::scale_color_manual(values = cols)+
theme_bw()
,color=colu2000[[11]]
ggplot(colu2000,aes(y=colu2000[[1]],x=colu2000[[11]],color = colu2000[[11]]))+
geom_point()+
geom_line()
ggplot(CSTJ,aes(y=NL,x=NDVI,color =`0010BHG2`))+
geom_point()+scale_color_manual(values = col)
my_palette <- colorRampPalette(c("red", "orange", "lightblue", "blue"))
ggpairs(colu2000, columns = colu2000[1:3],
mapping = ggplot2::aes(color = colu2000[11], shape = colu2000[11]),
lower = list(continuous = wrap("points", alpha = 0.3, size = 0.1)),
upper = list(continuous = wrap("cor", size = 2))) +
scale_fill_manual(values = col)
col <- c("#FF0000", "#FF7F00", "#007FFF", "#0000FF")
#简单热图-----------
corr <- cor %>% as_md_tbl()
head(corr)
p <- qcorrplot(corr) +
geom_square()+scale_fill_gradientn(colours = c4a('rd_bu',30))+
geom_mark(size = 2.8, #显著性标记大小
only_mark = T, #只显示显著性标记,逻辑值为F则会同时显示具体数值
sig_level = c(0.05, 0.01, 0.001), # 显著性水平
sig_thres = 0.05) + #设定显著性阈值
#添加对角线标签:
geom_diag_label(geom = "text", #"text", "label" or "richtext"
angle=0) +
#将Y轴标签字体颜色修改为白色隐藏:
theme(axis.text.y = element_text(color = "white"))
p
#组合简单热图----------
CSTJ$BH2G0010 <- factor(CSTJ$BH2G0010,levels = c("A2","A1","B","C1","C2"))
ggplot(CSTJ, aes(x=BH2G0010, y=CS2000, fill = BH2G0010)) + # 需要修改数据框(df)以及aes()函数内的参数
geom_half_violin(position = position_nudge(x = 0.25), side = "r", width = 0.8, color = NA) + # 部分需要调整的几何对象参数
geom_boxplot(width = 0.4, size = 1.2, outlier.color = NA) + # 部分需要调整的几何对象参数
geom_jitter(aes(fill = BH2G0010), shape = 21, size = 2.5, width = 0.2) + # 部分需要调整的几何对象参数
geom_hline(yintercept = 0, linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
geom_hline(yintercept = mean(CSTJ$CS2000), linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
geom_signif(comparisons = list(c("A1", "B"), # 需要修改比较的组合
c("A1", "C1"),
c("A2", "C2")),
map_signif_level = TRUE, # 参数值为TRUE,不是T
test = t.test,
y_position = c(1550, 1600, 1650),
tip_length = c(0, 0, 0, 0, 0, 0),
size = 1, color = "black", textsize = 7) + # 部分需要调整的统计显著测试参数
scale_y_continuous(limits = c(200,1800), breaks = c(200,600,1000,1400,1800)) + # 需要调整y轴标尺的范围和刻度
theme_bw() + #主题设置,可根据需要调整
theme(panel.grid = element_blank(),
panel.border = element_rect(size = 1),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black", size = 13),
legend.position = "none",
axis.ticks = element_line(color = "black", linewidth = 1)) + # 主题设置,可根据需要调整
labs(x = NULL, y = NULL) + # x轴标签和y轴标签
scale_fill_manual(values = c("#5cc3e8", "#ffdb00", "#79ceb8", "#e95f5c", "#e95f5c")) # 需要调整填充颜色
#散点+箱线图+小提琴图+占比-----
ggplot(CSTJ, aes(x=BH2G0010, y=NL, fill = BH2G0010)) + # 需要修改数据框(df)以及aes()函数内的参数
geom_half_violin(position = position_nudge(x = 0.25), side = "r", width = 0.8, color = NA) + # 部分需要调整的几何对象参数
geom_boxplot(width = 0.4, size = 1.2, outlier.color = NA) + # 部分需要调整的几何对象参数
geom_jitter(aes(fill = BH2G0010), shape = 21, size = 2.5, width = 0.2) + # 部分需要调整的几何对象参数
geom_hline(yintercept = 0, linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
geom_hline(yintercept = mean(CSTJ$NL), linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
scale_y_continuous(limits = c(0,65), breaks = c(0,20,40,60)) + # 需要调整y轴标尺的范围和刻度
theme_bw() + #主题设置,可根据需要调整
theme(panel.grid = element_blank(),
panel.border = element_rect(size = 1),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black", size = 13),
legend.position = "none",
axis.ticks = element_line(color = "black", linewidth = 1)) + # 主题设置,可根据需要调整
labs(x = NULL, y = NULL) + # x轴标签和y轴标签
scale_fill_manual(values = c("#5cc3e8", "#ffdb00", "#79ceb8", "#e95f5c", "#d78b5f"))+ # 需要调整填充颜色
geom_text(aes(x = "A2", label = paste0(round(A2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "A1", label = paste0(round(A1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "B", label = paste0(round(B_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C1", label = paste0(round(C1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C2", label = paste0(round(C2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5)
#平均值计算
above_NL <- CSTJ[CSTJ$NL > mean(CSTJ$NL), ]
# Step 2: 提取以上筛选结果中A2, A1, B1C1, C2的数量,并计算占比
A2_count <- sum(above_NL$BH2G0010 == "A2")
A1_count <- sum(above_NL$BH2G0010 == "A1")
B_count <- sum(above_NL$BH2G0010 == "B")
C1_count <- sum(above_NL$BH2G0010 == "C1")
C2_count <- sum(above_NL$BH2G0010 == "C2")
A2_total <- sum(CSTJ$BH2G0010 == "A2")
A1_total <- sum(CSTJ$BH2G0010 == "A1")
B_total <- sum(CSTJ$BH2G0010 == "B")
C1_total <- sum(CSTJ$BH2G0010 == "C1")
C2_total <- sum(CSTJ$BH2G0010 == "C2")
A2_percentage <- A2_count / A2_total * 100
A1_percentage <- A1_count / A1_total * 100
B_percentage <- B_count / B_total * 100
C1_percentage <- C1_count / C1_total * 100
C2_percentage <- C2_count / C2_total * 100
#散点+箱线图+小提琴图+辅助线+占比-----
ggplot(CSTJ, aes(x=BH2G0010, y=NL, fill = BH2G0010)) + # 需要修改数据框(df)以及aes()函数内的参数
geom_half_violin(position = position_nudge(x = 0.25), side = "r", width = 0.8, color = NA) + # 部分需要调整的几何对象参数
geom_boxplot(width = 0.4, size = 1.2, outlier.color = NA) + # 部分需要调整的几何对象参数
geom_jitter(aes(fill = BH2G0010), shape = 21, size = 2.5, width = 0.2) + # 部分需要调整的几何对象参数
geom_hline(yintercept = 0, linetype = 1, color = "#5cc3e8", linewidth = 1) + # 部分需要调整的参数
geom_hline(yintercept = mean(CSTJ$NL), linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
scale_y_continuous(limits = c(0,65), breaks = c(0,20,40,60)) + # 需要调整y轴标尺的范围和刻度
theme_bw() + #主题设置,可根据需要调整
theme(panel.grid = element_blank(),
panel.border = element_rect(size = 1),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black", size = 13),
legend.position = "none",
axis.ticks = element_line(color = "black", linewidth = 1)) + # 主题设置,可根据需要调整
labs(x = NULL, y = NULL) + # x轴标签和y轴标签
scale_fill_manual(values = c("#5cc3e8", "#ffdb00", "#79ceb8", "#e95f5c", "#d78b5f"))+ # 需要调整填充颜色
geom_text(aes(x = "A2", label = paste0(round(A2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "A1", label = paste0(round(A1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "B", label = paste0(round(B_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C1", label = paste0(round(C1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C2", label = paste0(round(C2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5)
#平均值计算
above_NL <- CSTJ[CSTJ$NL > mean(CSTJ$NL), ]
# Step 2: 提取以上筛选结果中A2, A1, B1C1, C2的数量,并计算占比
A2_count <- sum(above_NL$BH2G0010 == "A2")
A1_count <- sum(above_NL$BH2G0010 == "A1")
B_count <- sum(above_NL$BH2G0010 == "B")
C1_count <- sum(above_NL$BH2G0010 == "C1")
C2_count <- sum(above_NL$BH2G0010 == "C2")
A2_total <- sum(CSTJ$BH2G0010 == "A2")
A1_total <- sum(CSTJ$BH2G0010 == "A1")
B_total <- sum(CSTJ$BH2G0010 == "B")
C1_total <- sum(CSTJ$BH2G0010 == "C1")
C2_total <- sum(CSTJ$BH2G0010 == "C2")
A2_percentage <- A2_count / A2_total * 100
A1_percentage <- A1_count / A1_total * 100
B_percentage <- B_count / B_total * 100
C1_percentage <- C1_count / C1_total * 100
C2_percentage <- C2_count / C2_total * 100
#GDP---------
above_GDP <- CSTJ[CSTJ$GDP > mean(CSTJ$GDP), ]
# Step 2: 提取以上筛选结果中A2, A1, B1C1, C2的数量,并计算占比
A2_count <- sum(above_GDP$BH2G0010 == "A2")#谢泽阳
A1_count <- sum(above_GDP$BH2G0010 == "A1")
B_count <- sum(above_GDP$BH2G0010 == "B")
C1_count <- sum(above_GDP$BH2G0010 == "C1")
C2_count <- sum(above_GDP$BH2G0010 == "C2")
A2_total <- sum(CSTJ$BH2G0010 == "A2")
A1_total <- sum(CSTJ$BH2G0010 == "A1")
B_total <- sum(CSTJ$BH2G0010 == "B")
C1_total <- sum(CSTJ$BH2G0010 == "C1")
C2_total <- sum(CSTJ$BH2G0010 == "C2")
A2_percentage <- A2_count / A2_total * 100
A1_percentage <- A1_count / A1_total * 100
B_percentage <- B_count / B_total * 100
C1_percentage <- C1_count / C1_total * 100
C2_percentage <- C2_count / C2_total * 100
ggplot(CSTJ, aes(x=BH2G0010, y=GDP, fill = BH2G0010)) + # 需要修改数据框(df)以及aes()函数内的参数
geom_half_violin(position = position_nudge(x = 0.25), side = "r", width = 0.8, color = NA) + # 部分需要调整的几何对象参数
geom_boxplot(width = 0.4, size = 1.2, outlier.color = NA) + # 部分需要调整的几何对象参数
geom_jitter(aes(fill = BH2G0010), shape = 21, size = 2.5, width = 0.2) + # 部分需要调整的几何对象参数
geom_hline(yintercept = 0, linetype = 1, color = "#5cc3e8", linewidth = 1) + # 部分需要调整的参数
geom_hline(yintercept = mean(CSTJ$GDP), linetype = 2, color = "red", linewidth = 1) + # 部分需要调整的参数
# scale_y_continuous(limits = c(0,65), breaks = c(0,20,40,60)) + # 需要调整y轴标尺的范围和刻度
theme_bw() + #主题设置,可根据需要调整
theme(panel.grid = element_blank(),
panel.border = element_rect(size = 1),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black", size = 13),
legend.position = "none",
axis.ticks = element_line(color = "black", linewidth = 1)) + # 主题设置,可根据需要调整
labs(x = NULL, y = NULL) + # x轴标签和y轴标签
scale_fill_manual(values = c("#5cc3e8", "#ffdb00", "#79ceb8", "#e95f5c", "#d78b5f"))+ # 需要调整填充颜色
geom_text(aes(x = "A2", label = paste0(round(A2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "A1", label = paste0(round(A1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "B", label = paste0(round(B_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C1", label = paste0(round(C1_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5) +
geom_text(aes(x = "C2", label = paste0(round(C2_percentage, 1), "%")), y = 65, vjust = -0.5, size = 3.5)