3

Is it possible to change the font family of the p-value in the following ggpboxplot? The font family should be "Times New Roman".

Using theme_bw(base_family = "Times New Roman"), does not change the p-value.

library(ggpubr) 
library(ggplot2)
data("ToothGrowth")

# Box plot 
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
      color = "supp", palette = "jco",
      add = "jitter",
      facet.by = "dose", short.panel.labs = FALSE) 

p + stat_compare_means(label = "p.format")
p + theme_bw(base_family = "Times New Roman")

I also tried it with normal theme(text = ...). It does not work.

# Box plot 
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
      color = "supp", palette = "jco",
      add = "jitter",
      facet.by = "dose", short.panel.labs = FALSE) 

p + stat_compare_means(label = "p.format")
p + theme(text = element_text(family = "Times New Roman"))

Please note: I took the example from the following website: https://rpkgs.datanovia.com/ggpubr/reference/stat_compare_means.html

Thank you very, very much!

All the best, Aenna

AennaPhD
  • 147
  • 8

1 Answers1

3

You were close, the family = argument goes directly in stat_compare_means().

From help(stat_compare_means):

Usage
stat_compare_means(mapping = NULL, data = NULL, method = NULL,
  paired = FALSE, method.args = list(), ref.group = NULL,
  comparisons = NULL, hide.ns = FALSE, label.sep = ", ",
  label = NULL, label.x.npc = "left", label.y.npc = "top",
  label.x = NULL, label.y = NULL, tip.length = 0.03,
  bracket.size = 0.3, step.increase = 0, symnum.args = list(),
  geom = "text", position = "identity", na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE, ...)
<snip>
...   other arguments to pass to geom_text or geom_label.

Thus, this code should do what you want.

 ggboxplot(ToothGrowth, x = "supp", y = "len",
      color = "supp", palette = "jco",
      add = "jitter",
      facet.by = "dose", short.panel.labs = FALSE) +
      stat_compare_means(label = "p.format",family = "Times New Roman") 
      #Use this if on Windows:  stat_compare_means(label = "p.format",family = "TT Times New Roman")

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • 2
    Note: you may need to [consult here](https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2) like I did when I received error message that "font family not found in Windows font database" (Obviously, I'm on a Windows PC). – chemdork123 Apr 09 '20 at 15:22
  • Thanks for the heads up. I edited that into the answer. – Ian Campbell Apr 09 '20 at 15:35