【问题标题】:Change font family of stat_compare_mean() output in ggplot在 ggplot 中更改 stat_compare_mean() 输出的字体系列
【发布时间】:2020-04-09 14:25:40
【问题描述】:

是否可以更改以下 ggpboxplot 中 p 值的字体系列?字体系列应为“Times New Roman”。

使用 theme_bw(base_family = "Times New Roman"),不会改变 p 值。

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")

我也尝试过使用普通主题(文本 = ...)。它不起作用。

# 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"))

请注意:我从以下网站获取示例: https://rpkgs.datanovia.com/ggpubr/reference/stat_compare_means.html

非常非常感谢!

一切顺利, 艾娜

【问题讨论】:

    标签: r ggplot2 font-family


    【解决方案1】:

    你已经接近了,family = 参数直接进入stat_compare_means()

    来自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.
    
    

    因此,这段代码应该做你想做的事。

     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")
    

    【讨论】:

    • 注意:您可能需要consult here,就像我收到错误消息“在 Windows 字体数据库中找不到字体系列”时所做的那样(显然,我在 Windows PC 上)。跨度>
    • 感谢您的提醒。我将其编辑为答案。
    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 2016-10-03
    • 1970-01-01
    • 2019-04-26
    • 2020-06-26
    • 2023-03-27
    • 2013-07-29
    • 1970-01-01
    相关资源
    最近更新 更多