1

我是 R 和 stackoverflow 的新手

我一直在寻找在配对图中呈现 R^2 和 P 值的代码,特别是对于 gam(上)和 lm(下)。但是,我坚持使用此代码:

library(tidyverse) # plotting and manipulation
library(grid) # combining plots
library(gridExtra) # combining plots
library(ggpubr) # combining plots
library(patchwork) # combining plots
library(ggfortify) # nice extension for ggplot
library(mgcv) #fitting gam models
library(GGally) # displaying pairs panel

sel1<-select(rekap,c('Total_Ni', 'Total_Mg', 'Total_Fe', 'CEC', 'pH','SWC'))## selecting column

str(sel1)
tibble [36 x 6] (S3: tbl_df/tbl/data.frame)
 $ Total_Ni: num [1:36] 1750 1565 1249 853 959 ...
 $ Total_Mg: num [1:36] 1468 1558 1164 813 915 ...
 $ Total_Fe: num [1:36] 381 300 172 173 144 ...
 $ CEC     : num [1:36] 105 132 117 118 141 ...
 $ pH      : num [1:36] 4.21 4.22 4.49 4.43 4.05 4.09 5.21 5.27 4.32 4.29 ...
 $ SWC     : num [1:36] 435 511 497 517 621 ...

## Build function for upper and lower plots
my_fn1 <- function(data, mapping, method="gam", ...){
      p <- ggplot(data = sel1, mapping = mapping) + 
      geom_point() + 
      geom_smooth(method=method,colour="blue", ...)
      p
    }       
my_fn2 <- function(data, mapping, method="lm", ...){
      p2 <- ggplot(data = sel1, mapping = mapping) + 
      geom_point() + 
      geom_smooth(method=method,colour="orangered2", ...)
      p2
    }   

##Pairing with ggpairs
p1 <- ggpairs(sel1, columnLabels = c("Total Ni", "Total Mg", "Total Fe", "CEC", "pH","SWC"),
            upper=list(continuous =my_fn1),
            lower=list(continuous =my_fn2))+ 
            theme_bw() + theme(axis.text.x=(element_text(size=rel(0.7), angle=0)),
            axis.text.y=(element_text(size=rel(0.7), angle=0)), panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA,colour = "grey35"))   

这里的结果, Rplot

我一直在尝试构建更合适的代码,但没有成功返回。

4

1 回答 1

2

更新: 这是一个起点,而不是完整的解决方案。主要问题是要访问 gam 模型中的 R² 和 p.value。不幸的是,在我的解决方案中,来自 lm 模型的 R² 和 p 应用于 gam 模型。

经过一些研究,我发现这个Gettingadjusted r-squared value for each line in a geom_smooth gam and

stat_fit_glance 和广义加法模型 (GAM) 错误

我将 sel1 替换为模拟数据:我们正在使用stat_corfrom ggpubr

library(tidyverse) # plotting and manipulation
library(grid) # combining plots
library(gridExtra) # combining plots
library(ggpubr) # combining plots
library(patchwork) # combining plots
library(ggfortify) # nice extension for ggplot
library(mgcv) #fitting gam models
library(GGally) # displaying pairs panel

#sel1<-select(rekap,c('Total_Ni', 'Total_Mg', 'Total_Fe', 'CEC', 'pH','SWC'))## selecting column
mtcars1 <- mtcars %>% select(1:6)

mtcars1
## Build function for upper and lower plots
my_fn1 <- function(data, mapping, method="gam", ...){
  p <- ggplot(data = mtcars1, mapping = mapping) + 
    geom_point() + 
    geom_smooth(method=method,colour="blue", ...)+
    stat_cor(aes(label=paste("Pearson",..rr.label.., ..p.label.., sep = "~`,`~")), 
             method="pearson", label.y = 0)
  p
}  

my_fn2 <- function(data, mapping, method="lm", ...){
  p2 <- ggplot(data = mtcars1, mapping = mapping) + 
    geom_point() + 
    geom_smooth(method=method,colour="orangered2", ...)+
    stat_cor(aes(label=paste("Pearson",..rr.label.., ..p.label.., sep = "~`,`~")), 
             method="pearson", label.y = 0)
  p2
}   

p1 <- ggpairs(mtcars1, columnLabels = c("Total Ni", "Total Mg", "Total Fe", "CEC", "pH","SWC"),
              upper=list(continuous =my_fn1),
              lower=list(continuous =my_fn2))+ 
  theme_bw() + theme(axis.text.x=(element_text(size=rel(0.7), angle=0)),
                     axis.text.y=(element_text(size=rel(0.7), angle=0)), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA,colour = "grey35")) 
p1

在此处输入图像描述

于 2021-10-31T14:22:24.450 回答