Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Displaying correlation coefficients in bold with ggpubr Stat_cor

I use stat_cor() to display the pearson correlation coefficients on a boxplot. To favor the visibility of the coefficients, I am trying to display them in bold. I am still quite new to R, and I tried to solve the issue by myself with this code :

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
  geom_point(aes(color = Species)) +
  stat_smooth(method = lm, aes(color = Species), se = FALSE) +
  stat_cor(aes(color = Species, label = after_stat(r.label)), 
  r.accuracy = 0.01, label.x = 4, label.y = c(8, 7.8, 7.6), 
  fontface = "bold") +
  scale_color_manual(values = c("grey70", "gold", "green3")) +
  theme_classic() +
  theme(
        legend.background = element_rect(fill="grey90", 
                                         size=0.5, linetype="solid"))

This method didn’t work, and I couldn’t figure out how to do it in theme(), even though I believe there is probably a way.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As a general rule: The theme() will have no effect on text labels. It sets the overall look of the plot and will affect only the so-called non-data ink. For text labels you have to set the font face in the geom or the stat. However, stat_cor uses ?plotmath expressions to create the labels, i.e. make the R or … italic. And in that the face of the labels can’t be set or changed via fontface=.

Hence, to make the labels bold requires to create the label yourself inside after_stat:

library(ggplot2)
library(ggpubr)

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
  geom_point(aes(color = Species)) +
  stat_smooth(method = lm, aes(color = Species), se = FALSE) +
  stat_cor(
    aes(
      color = Species,
      label = after_stat(
        paste0(
          "bolditalic(R)~",
          "bold(`=`)~",
          "bold('", r, "')"
        )
      )
    ),
    r.accuracy = 0.01,
    label.x = 4, label.y = c(8, 7.8, 7.6),
  ) +
  scale_color_manual(values = c("grey70", "gold", "green3")) +
  theme_classic() +
  theme(
    legend.background = element_rect(
      fill = "grey90",
      size = 0.5, linetype = "solid"
    )
  )
#> Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> `geom_smooth()` using formula = 'y ~ x'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading