Home > Software design >  Add p-value and/or 95% CI in the text of a correlation matrix plot
Add p-value and/or 95% CI in the text of a correlation matrix plot

Time:01-10

Is it possible to add in the text of the correlation matrix plot the exact p-value and/or 95% CI, next to where the point estimate is mentioned, by adding something to this code?

enter image description here

I welcome solutions using other packages too.

 ggpairs(baseline2, title="correlogram ") 

df

baseline2 <- structure(list(delta_bp = c(-21.5, 7, -17.5, -10.5, -21, -7.5, 
4.5, 3, -9, 9, -22.5, -9.5), delta_bp_05 = c(-21.5, 0, 3, -13.5, 
-13, -4, -16.5, -8, 5, -5, -12, 0.5), delta_bp_10 = c(-26.5, 
1, -6, -10.5, -9, -3, -20.5, -10, 1, -6, -22, -0.5)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`17` = 17L, 
`27` = 27L, `32` = 32L), class = "omit"))

CodePudding user response:

You can use the ggally_statistic function via the "statistic" option to upper and supply your own text_fn. The function you give to text_fn takes the data columns and should return the text string to display. Here it is with the p-value and confidence interval added to the correlation. This can be customised however you like.

tf = function(x,y) {
  ct <- cor.test(x, y)
  sprintf("Corr: %0.2f, p: %0.3f\n95%% CI: (%0.2f to %0.2f)", 
          ct$estimate, ct$p.value, ct$conf.int[1], ct$conf.int[2])
}

ggpairs(baseline2, title="correlogram ", 
        upper=list(continuous=wrap("statistic",text_fn=tf,title=NULL, sep=NULL)))

enter image description here

  • Related