rm(list = ls())
###############################input data 
library(ggplot2)
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- list.files(pattern = ".*csv",dir_path,full.names = T, recursive = F)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data.csv"
data_6 <- read.csv(grep("data.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
data_6$Mapping.ID <- NULL
colnames(data_6) <- c("xx", "yy")
data_6 <- data_6[1:50,]
head(data_6)
##          xx        yy
## 1 -5.171266 -5.130767
## 2 -5.254599 -5.514100
## 3 -5.254599 -5.230767
## 4 -5.271266 -5.164100
## 5 -5.154599 -5.247433
## 6 -5.354599 -5.114100
p1 <- ggplot(data_6, aes(x = xx, y = yy)) + 
  geom_point(size=1,color="darkblue",alpha = 0.6) +
  geom_smooth(method=lm, color="#CE3D32FF",formula = y ~ x) +
  labs(title="",
       x="xx", y = "yy") +
  theme(panel.spacing = unit(0.1, "cm"),
        legend.position= "none",
        axis.ticks = element_line(colour = "black", 
                                  size = 0.5, linetype = "solid"),
        axis.line = element_line(colour = "black", 
                                 size = 0.5, linetype = "solid"),
        axis.text =element_text(face="plain", color="black", family = "sans",
                                size=14,angle = 0),
        panel.background = element_rect(fill = "white",
                                        colour = "white",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 1, linetype = 'dashed',
                                        colour = "white"),
        axis.title = element_text(color="black", size=14, face="plain",family="sans"))

library(ggpmisc)
## Warning: package 'ggpmisc' was built under R version 4.0.5
## Loading required package: ggpp
## Warning: package 'ggpp' was built under R version 4.0.5
## 
## Attaching package: 'ggpp'
## The following object is masked from 'package:ggplot2':
## 
##     annotate
p1 + stat_poly_eq(formula = y~x,
                  eq.with.lhs = "italic(hat(y))~`=`~",
                  aes(label = paste(..rr.label..)), 
                  parse = TRUE) 

cor(data_6$xx,data_6$yy)^2
## [1] 0.7931379
#install.packages("ggpmisc")
##########################output
ggsave(filename = paste0(Sys.Date(),"-xxyy.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 11, height = 11, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")
###########method_2
head(data_6)
##          xx        yy
## 1 -5.171266 -5.130767
## 2 -5.254599 -5.514100
## 3 -5.254599 -5.230767
## 4 -5.271266 -5.164100
## 5 -5.154599 -5.247433
## 6 -5.354599 -5.114100
df <- data_6
colnames(df) <- c("y", "x")
lm_eqn <- function(df){
  m <- lm(y ~ x, df);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                   list(a = format(unname(coef(m)[1]), digits = 2),
                        b = format(unname(coef(m)[2]), digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 2)))
  as.character(as.expression(eq));
}
p1 + geom_text(x = -6, y = -4.8, label = lm_eqn(df), parse = TRUE)

###########method_3

format(cor(data_6$xx,data_6$yy)^2, digits = 2)  # "0.75"
## [1] "0.79"
p1 + geom_text(x = -6, y = -4.8,color = "red", label = paste0(expression(italic("R")^2 == "0.75")), parse = TRUE)

#ref https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph