Moh Zulfajrin
Moh Zulfajrin

Reputation: 97

Adding R^2 and P-value in ggpairs (GGally) composed of LM and GAM scatterplot

i'm new to R and stackoverflow

I've been looking for the code to present R^2 and P-value in paired graph particularly for gam (upper) and lm (lower). However, i stuck with this code:

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"))   

Here the results, Rplot

I've been trying for long time to construct more appropriate code, but returned without success.

Upvotes: 1

Views: 739

Answers (1)

TarJae
TarJae

Reputation: 78947

Update: This is a starting point, not the complete solution. Main problem is that to access the R² and p.value in the gam model. Unfortunately in my solution R² and p from lm model is applied to gam model.

After some research I found this Getting adjusted r-squared value for each line in a geom_smooth gam and

stat_fit_glance and generalized additive models (GAM) error.

I replace sel1 with mock data: We are using stat_cor from 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

enter image description here

Upvotes: 2

Related Questions