重访ggplot2中stat_compare_means()的字体调整问题:手动“绘制”文本?

问题描述 投票:0回答:1

这是另一位用户的问题的后续问题。使用

comparisons()
参数时无法更改字体是
stat_compare_means()
的已知限制。

下面的 MWE 描述了最初的问题:

library(ggplot2)
library(ggpubr) 
library(showtext)
tooth <- subset(ToothGrowth, dose == 0.5)
font_add_google("Libre Baskerville")
showtext_auto()

ggboxplot(tooth, x = "supp", y = "len")+
  theme_classic(base_size = 20, base_family = "Libre Baskerville") +
  stat_compare_means(comparisons = list(c("OJ", "VC")),
                     label = "p.format",
                     family = "Libre Baskerville",
                     size = 5)

创建于 2024-02-14,使用 reprex v2.1.0

我尝试通过在

geom_rect()
生成的文本上手动添加
stat_compare_means()
对象来解决此问题,打算稍后手动添加具有所需字体的文本。然而,比较括号却不断“逃走”。

我想删除字体错误的文本,同时保留比较括号。


ggboxplot(tooth, x = "supp", y = "len")+
  theme_classic(base_size = 20, base_family = "Libre Baskerville") +
  stat_compare_means(comparisons = list(c("OJ", "VC")),
                     label = "p.format",
                     family = "Libre Baskerville",
                     size = 5) +
  geom_rect(aes(xmin = 1, xmax = 2, ymin = 23, ymax = 26), fill = "white",
           linetype = "dashed", color = "red")

创建于 2024-02-14,使用 reprex v2.1.0

r ggplot2 fonts boxplot showtext
1个回答
0
投票

有一个更简单的方法可以解决这个问题。

首先,创建你的情节:

p <- ggboxplot(tooth, x = "supp", y = "len")+
  theme_classic(base_size = 20, base_family = "Libre Baskerville") +
  stat_compare_means(comparisons = list(c("OJ", "VC")),
                     label = "p.format",
                     family = "Libre Baskerville",
                     size = 5)

现在直接把姓氏写进第二层的

aes_parameters

p$layers[[2]]$aes_params$family <- "Libre Baskerville"

现在 p 值具有您选择的字体:

p

© www.soinside.com 2019 - 2024. All rights reserved.