ggobi/ggally

ggcorr does not support "non syntactically valid" column names.

trekonom opened this issue · 2 comments

When answering this question on SO I stumbled over an issue with ggcorr when column names are not syntactically valid variable names. In that case the column names are converted to syntactically valid names and all "special" symbols are replaced by dots.

A minimal reproducible example of the issue:

ex_db <- structure(list(
  SAPS = c(11L, 14L, 14L, 15L, 14L, 13L, 14L, 13L, 12L, 15L),
  `e'` = c(119, 62, 74, 75, 111, 66, 102, 71, 100, 108),
  `E/e'` = c(50, 111, 82, 68, 78, 105, 60, 91, 61, 49)
), class = "data.frame", row.names = c(NA, -10L))

library(GGally)
#> Loading required package: ggplot2
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2

ggcorr(ex_db)

Created on 2023-05-11 with reprex v2.0.2

IMHO the issue is that when converting the correlation matrix to a dataframe using data.frame() the default check.names=TRUE is used:

m = data.frame(m * lower.tri(m))

A "hacky" workaround would be to manipulate the ggplot object returned by ggcorr and to replace the diagLabel column containing the labels with the original column names.

Note: This requires to identify the correct geom_text layer which adds the labels stored in the diagLabel column, i.e. the one with mapping: label = ~diagLabel .

p <- ggcorr(ex_db)

p$layers[[2]]$data[c("diagLabel")] <- names(ex_db)

p

Created on 2023-05-11 with reprex v2.0.2