How to indicating the estimated regression and p value in the ggplot2

Hi,
I did linear regression analysis between the response variable(y) and
predictor variables in the surgical data set considering pindex as a confounding variable.
I aim to plot the response variable(y) against the experimentally determined values of the predictor variables and to this end, I am Successful. However, could not able to indicate the estimated regression and p-value in the ggplot2
In the code below, trying to do the analysis and the plot.
It would be much appreciated if someone could show me how to indicate the estimated regression and p-values in the ggplot2.

library(olsrr)
library(tidyverse)
library(reshape2)
data(surgical)

## Regression 
pre1 <-setdiff(names(surgical), c("y", "pindex"))
mod_dres<-NULL
for (j in pre1) {
  model  <- lm(y  ~  pindex + get(j), data = surgical)
  bmodel <- broom::tidy(model)
  bmodel$term[3]<-j
  bmodel<-bmodel[3,]
  mod_dres<-rbind(mod_dres,bmodel)
}
mod_dres

## Matching the significant variables with the orginal data and reshaping  
pre1.plot = melt(surgical[,-c(2)], id.vars='y') %>% 
  dplyr::filter(variable %in% mod_dres$term) 

## plot the predictors varaibles 
ggplot(pre1.plot) +
  geom_jitter(aes(value,y, colour=variable), 
              colour="darkorange", size = 3) + 
  geom_smooth(aes(value,y, colour=variable), 
              method=lm, se=FALSE, colour="darkorange") +
  theme_minimal() +
  theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
        axis.text = element_text(size = 16,face="bold", colour = "black"),
        axis.line = element_line(colour='black'),
        axis.ticks = element_line(colour='black'),
        plot.title = element_text(hjust = 0.5,size=18,face="bold"),
        legend.position = "bottom",
        legend.title = element_text(color = "Black", size = 9, face = "bold"),
        legend.text=element_text(color = "Black", size = 9, face = "bold"),
        strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
  labs(x = "value",title = " ") +
  facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) + 
  theme(panel.background = element_rect(fill = "white", colour = "black"),
        strip.background = element_rect(fill = "white", colour = "black"),
        legend.key = element_blank(),
        legend.title = element_blank())

Thank you!

Illustrates retrieving model output and putting into the plot title.

library(ggplot2)

# create model

fit <- lm(mpg ~ hp, data = mtcars)

# see what model information is available

stats <- summary(fit)
str(stats)
#> List of 11
#>  $ call         : language lm(formula = mpg ~ hp, data = mtcars)
#>  $ terms        :Classes 'terms', 'formula'  language mpg ~ hp
#>   .. ..- attr(*, "variables")= language list(mpg, hp)
#>   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:2] "mpg" "hp"
#>   .. .. .. ..$ : chr "hp"
#>   .. ..- attr(*, "term.labels")= chr "hp"
#>   .. ..- attr(*, "order")= int 1
#>   .. ..- attr(*, "intercept")= int 1
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. ..- attr(*, "predvars")= language list(mpg, hp)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#>   .. .. ..- attr(*, "names")= chr [1:2] "mpg" "hp"
#>  $ residuals    : Named num [1:32] -1.594 -1.594 -0.954 -1.194 0.541 ...
#>   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#>  $ coefficients : num [1:2, 1:4] 30.0989 -0.0682 1.6339 0.0101 18.4212 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "hp"
#>   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
#>  $ aliased      : Named logi [1:2] FALSE FALSE
#>   ..- attr(*, "names")= chr [1:2] "(Intercept)" "hp"
#>  $ sigma        : num 3.86
#>  $ df           : int [1:3] 2 30 2
#>  $ r.squared    : num 0.602
#>  $ adj.r.squared: num 0.589
#>  $ fstatistic   : Named num [1:3] 45.5 1 30
#>   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
#>  $ cov.unscaled : num [1:2, 1:2] 1.79e-01 -1.01e-03 -1.01e-03 6.86e-06
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "hp"
#>   .. ..$ : chr [1:2] "(Intercept)" "hp"
#>  - attr(*, "class")= chr "summary.lm"

# some already have an extractor function

coef(fit)
#> (Intercept)          hp 
#> 30.09886054 -0.06822828

# others you can access indirectly with this trick

inside <- print(summary(fit))
#> 
#> Call:
#> lm(formula = mpg ~ hp, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.7121 -2.1122 -0.8854  1.5819  8.2360 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
#> hp          -0.06823    0.01012  -6.742 1.79e-07 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.863 on 30 degrees of freedom
#> Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
#> F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07
coef(inside)
#>                Estimate Std. Error   t value     Pr(>|t|)
#> (Intercept) 30.09886054  1.6339210 18.421246 6.642736e-18
#> hp          -0.06822828  0.0101193 -6.742389 1.787835e-07
coef(inside)[8]
#> [1] 1.787835e-07

headline <- paste("My model: coefficient and p-value",coef(fit)[2],coef(inside)[8])

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) + 
  ggtitle(headline) +
  theme_minimal()
#> `geom_smooth()` using formula 'y ~ x'

@technocrat , thanks for your replay.

Your solution is nice but I want those values inside the plot not as title.

Best,
Amare

Use geom_text for that

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.