Is there a way to change a font style for correlation shown in a correlation plot created using ggcorplot? I understand one can change the size and color of correlation, but I am having difficulty finding out a solution to change a font style.
e.g., correlation between drat and mpg is 0.7 and I would like to change 0.7 into a different font styl (e.g., from Arial to Times New Roman).
library(ggcorrplot)
# Compute a correlation matrix
data(mtcars)
corr <- round(cor(mtcars), 1)
# Compute a matrix of correlation p-values
p.mat <- cor_pmat(mtcars)
# Compute a plot
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
lab = TRUE)
The resulting plot looks like this:
CodePudding user response:
One option would be to do some hacks or manipulations on the ggplot
object returned by ggcorrplot
, i.e. you could override the default mapping
used by the geom_text
layer (which is the second layer) which add the text label, e.g. below I use an ifelse
to set the fontface
to "bold" for drat and mpg and "plain" otherwise:
library(ggcorrplot)
#> Loading required package: ggplot2
data(mtcars)
corr <- round(cor(mtcars), 1)
p <- ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
lab = TRUE
)
p$layers[[2]]$mapping <- aes(Var1, Var2,
fontface = ifelse(Var2 == "mpg" & Var1 == "drat", "bold", "plain")
)
p