library(ggplot2)
library(ggpmisc)
library(ggpubr)
library(RColorBrewer)
library(grid)
library(scales)
library(readxl)
library(plotly)
library(showtext)

# 添加Times New Roman字体路径（确保字体已安装在系统中）
font_add("Times New Roman", "C:/Windows/Fonts/times.ttf")  # 确保字体路径正确
showtext_auto()  # 自动应用 showtext

# 清空全局环境
rm(list=ls())

# 读取Excel数据
df <- read_excel("C:/Users/ZhuanZ/Desktop/大论文/副本A.xlsx")
head(df)

# 自定义颜色
col <- c("#1e90ff", "#cb78a6", "#fe0076", "#28e5a1", "#ff6347", 
         "#00ff00", "#ffa500", "#800080")

library(dplyr)
df <- df %>%
  mutate(group = str_trim(group))  # 清除 group 中的空格

# 绘制图形
p2 <- ggplot(df, aes(x = y, y = x, color = group)) +
  geom_point(size = 3, alpha = 1) +  # 绘制散点图
  scale_color_manual(values = col) +  # 设置颜色
  theme_bw() +  # 使用黑白主题
  theme(
    axis.text = element_text(family = "Times New Roman", color = 'black', size = 12),
    legend.text = element_text(family = "Times New Roman", color = 'black', size = 12),
    legend.title = element_blank(),
    axis.text.x = element_text(family = "Times New Roman", size = 12),
    axis.text.y = element_text(family = "Times New Roman", size = 12),
    axis.title = element_text(family = "Times New Roman", size = 15, face = "bold"),
    strip.text = element_text(family = "Times New Roman", size = 15, face = "bold"),
    plot.title = element_text(family = "Times New Roman", size = 16, face = "bold"),
    plot.subtitle = element_text(family = "Times New Roman", size = 14),
    axis.title.x = element_text(size = 20, margin = margin(t = 20, r = 0, b = 0, l = 0), family = "Times New Roman", face = "bold"),
    axis.title.y = element_text(size = 20, margin = margin(t = 20, r = 20, b = 10, l = 20), family = "Times New Roman", face = "bold"),
  ) +
  labs(x = bquote(bold("observed AGB (Mg/ha)")), y = bquote(bold("predicted AGB (Mg/ha)"))) +
  facet_grid(factor(group) ~ factor(LOL), scales = 'free') +
  scale_x_continuous(breaks = seq(0, 200, by = 50), expand = c(0, 0), lim = c(50, 250)) +
  scale_y_continuous(breaks = seq(0, 200, by = 50), expand = c(0, 0), lim = c(50, 250)) +
  #scale_x_continuous(breaks = seq(0, 20, by = 2), expand = c(0, 0), lim = c(10, 22)) +
  #scale_y_continuous(breaks = seq(0, 20, by = 2), expand = c(0, 0), lim = c(10, 22)) +
  geom_segment(aes(x = -Inf, y = -Inf, xend = Inf, yend = Inf), 
               color = "black", 
               linetype = "dashed", 
               size = 0.3)  +  # 调整线条粗细+  # 添加虚线
  geom_text(data = df[!duplicated(df[, c("group", "LOL")]), ], 
            aes(x = 50, y = 50, 
                label = sprintf("R² = %.2f", R2) %>% gsub("-", "\u2212", .)), 
            vjust = -8.8, hjust = -0.1, size = 5, color = "black", family = "Times New Roman") +  # 添加R²注释
  geom_text(data = df[!duplicated(df[, c("group", "LOL")]), ], 
            aes(x = 50, y = 50, 
                label = sprintf("RMSE = %.2f", RMSE) %>% gsub("-", "\u2212", .)), 
            vjust = -7.5, hjust = -0.07, size = 5, color = "black", family = "Times New Roman") +  # 添加RMSE注释
  guides(color = FALSE)

# 显示图形
p2
# 导出图形为 PDF 文件
ggsave("C:/Users/ZhuanZ/Desktop/大论文/A.pdf", plot = p2, width = 15, height = 10, units = "in", dpi = 300)

