在 ggrepel 标签中使用 plotmath

using plotmath in ggrepel labels

我正在尝试创建一个绘图,我想在其中使用 ggrepel 包显示线性模型中的所有系数及其在每个点附加的各自统计细节。我已经设法创建了基本图,但我无法弄清楚的是如何在创建标签时使用 plotmath。因此,例如,在下面生成的图中,我想对 t 值 (t) 和 p 值 (p).此外,如果我要包括估计值,我可能还想在标签中包括希腊字母 beta (β)。

# loading needed libraries
library(ggrepel)
#> Loading required package: ggplot2
library(ggplot2)
library(GGally)
library(tidyverse)

# creating a dataframe containing results
(label_df <- broom::tidy(x = stats::lm(data = mtcars, wt ~ am*cyl), conf.int = TRUE) %>%
  dplyr::filter(.data = ., term != "(Intercept)") %>%
  dplyr::select(.data = ., term, estimate, conf.low, conf.high, statistic, p.value) %>%
  purrrlyr::by_row(
    .d = .,
    ..f = ~ paste(
      "t = ",
      round(.$statistic, digits = 3),
      ", p = ",
      round(.$p.value, digits = 3),
      sep = ""
    ),
    .collate = "rows",
    .to = "label",
    .labels = TRUE
  )
)
#> # tibble [3 x 7]
#>   term   estimate conf.low conf.high statistic  p.value label             
#>   <chr>     <dbl>    <dbl>     <dbl>     <dbl>    <dbl> <chr>             
#> 1 am      -0.956    -2.58      0.668    -1.21  0.238    t = -1.206, p = 0~
#> 2 cyl      0.304     0.135     0.473     3.68  0.000989 t = 3.678, p = 0.~
#> 3 am:cyl   0.0328   -0.234     0.300     0.252 0.803    t = 0.252, p = 0.~

# creating the model coefficient plot using ggcoef
plot <- GGally::ggcoef(x = stats::lm(data = mtcars, wt ~ am*cyl), exclude_intercept = TRUE)

# adding labels using ggrepel
plot +
  ggrepel::geom_label_repel(
    data = label_df,
    mapping = ggplot2::aes(x = estimate, y = term, label = label),
    size = 3,
    box.padding = grid::unit(x = 0.75, units = "lines"),
    fontface = "bold",
    direction = "y",
    color = "black",
    label.size = 0.25,
    segment.color = "black",
    segment.size = 0.5,
    segment.alpha = NULL,
    min.segment.length = 0.5,
    max.iter = 2000,
    point.padding = 0.5,
    force = 2,
    na.rm = TRUE
  )

如果我使用 base::substitutebase::bquote 之类的东西在 purrrlyr 中创建标签,我会收到以下错误:

.f must return either data frames or vectors for non-list collation

我可以通过将其转换为字符类型来消除此错误,但标签会变得一团糟。

# creating a dataframe containing results
(label_df <- broom::tidy(x = stats::lm(data = mtcars, wt ~ am*cyl), conf.int = TRUE) %>%
  dplyr::filter(.data = ., term != "(Intercept)") %>%
  dplyr::select(.data = ., term, estimate, conf.low, conf.high, statistic, p.value) %>%
  purrrlyr::by_row(
    .d = .,
    ..f = ~ as.character(bquote(
      "t = "~.(round(.$statistic, digits = 3))~
        ", p = "~
        .(round(.$p.value, digits = 3))
    )),
    .collate = "rows",
    .to = "label",
    .labels = TRUE
  )
)
#> # tibble [9 x 8]
#>   term   estimate conf.low conf.high statistic  p.value  .row label       
#>   <chr>     <dbl>    <dbl>     <dbl>     <dbl>    <dbl> <int> <chr>       
#> 1 am      -0.956    -2.58      0.668    -1.21  0.238        1 ~           
#> 2 am      -0.956    -2.58      0.668    -1.21  0.238        1 "\"t = \" ~~
#> 3 am      -0.956    -2.58      0.668    -1.21  0.238        1 0.238       
#> 4 cyl      0.304     0.135     0.473     3.68  0.000989     2 ~           
#> 5 cyl      0.304     0.135     0.473     3.68  0.000989     2 "\"t = \" ~~
#> 6 cyl      0.304     0.135     0.473     3.68  0.000989     2 0.001       
#> 7 am:cyl   0.0328   -0.234     0.300     0.252 0.803        3 ~           
#> 8 am:cyl   0.0328   -0.234     0.300     0.252 0.803        3 "\"t = \" ~~
#> 9 am:cyl   0.0328   -0.234     0.300     0.252 0.803        3 0.803

reprex package (v0.2.0) 创建于 2018-06-13。

根据评论中的讨论,您需要正确使用数学注释以避免错误,请参阅link

下面的标签格式适合我,包括带有希腊符号的 beta 估计值。 list 需要在 plotmath.

中获取逗号
(label_df <- broom::tidy(x = stats::lm(data = mtcars, wt ~ am*cyl), conf.int = TRUE) %>%
    dplyr::filter(.data = ., term != "(Intercept)") %>%
    dplyr::select(.data = ., term, estimate, conf.low, conf.high, statistic, p.value) %>%
    purrrlyr::by_row(
      .d = .,
      ..f = ~ paste(
        "list(italic(t)==",
        round(.$statistic, digits = 3),
        ", ~italic(p)==",
        round(.$p.value, digits = 3),
        ", ~beta==",
        round(.$estimate, digits = 3),
        ")",
        sep = ""
      ),
      .collate = "rows",
      .to = "label",
      .labels = TRUE
    )
)