如何在 ggplot2 stat_poly_eq 函数中加粗自定义方程?

How to bold custom equations in ggplot2 stat_poly_eq function?

我需要在 ggplot2 的 stat_poly_eq() 中加粗方程。我如何与 atop 函数一起执行此操作?

另外,有没有指定stat_poly_eq的坐标?

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = my.formula, 
    aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
    label.y = 0.9,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

不知道你能不能;您可以在等式下划线,例如

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = x ~ y, 
    aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
    label.y = 0.9,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

然而,我尝试的所有方法(例如 bold()bolditalic())在与 atop() 结合使用时都不起作用。可能与 this statement in the docs:

有关

Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants.

即使您使用例如“加粗”所有内容 theme_classic(base_family = "Arial Bold"),方程式和 R2 值不变。

--

在定位标签方面,您可以使用 label.xlabel.y 来移动它们,例如(label.y = 1 将标签放在图的顶部):

library(tidyverse)
library(ggpmisc)
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = x ~ y, 
    aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
    label.y = 1,
    label.x = 0.2,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()