Home > Back-end >  Relabelling facets using ggpairs
Relabelling facets using ggpairs

Time:01-25

I am using this code:

printVar = function(x,y){
  vals = cor.test(x,y,
                  method="spearman")[c("estimate","p.value")]
  names(vals) = c("rho","p")
  paste(names(vals),signif(unlist(vals),2),collapse="\n")
}


ggpairs(baseline_sbp, title=" ", 
        upper=list(continuous=wrap("statistic",text_fn=printVar,title=NULL, 
                                   sep=NULL  )    )   ) 

Question:

What can I add to the previous code to relabel the names on the facets? That is, to change "delta_bp" to "dragon" for example in the plot?

DF:

baseline_sbp <- 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 do this directly in ggpairs by naming the columns like so: columnLabels = c(" Dragon", "delta_bp_05", "delta_bp_10").

ggpairs(baseline_sbp,
  columnLabels = c(" Dragon", "delta_bp_05", "delta_bp_10"),
  title = " ",
  upper = list(continuous = wrap("statistic",
    text_fn = printVar, title = NULL,
    sep = NULL
  ))
)

CodePudding user response:

You could just use dplyr::rename within the ggpairs call:

ggpairs(baseline_sbp |> dplyr::rename(dragon=delta_bp), 
        title=" ", 
        upper=list(continuous=wrap("statistic",text_fn=printVar,title=NULL,sep=NULL))
        )       

enter image description here

  • Related