1

我试图在 r/ggplot2 中制作多面图,显示使用 ggpmisc 包生成的 R^2 和 P 值,但根据 P 值是否低于某个数字来格式化每一行。我可以使用单个字符“R”和“P”成功地做到这一点,但我无法获得 R^2 中的上标 2、等号或格式化的值本身。可能使事情复杂化的是,我希望将值四舍五入到小数点后 3 位,我可以这样做,但我再次无法应用格式。这是问题代码和输出:

编辑:my.formula <- y ~ x在代码开头添加了一行代码以使其工作。

library(tidyverse)
library(ggpmisc)

##edit: I left out this very important line previously (Thank you for pointing this out)
my.formula <- y ~ x  

##data (sorry for all the extra whitespace, have never known how to remove this
ex1 <- structure(list(time = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
                                       .Label = c("early", "late"), class = "factor"),
      x = c(0.321386372587633, 0.321386372587633, 0.321386372587633, 0.321386372587633, 0.321386372587633, 
            0.344034242910647, 0.344034242910647, 0.344034242910647, 0.344034242910647, 0.344034242910647,
            0.339242868568382, 0.339242868568382, 0.339242868568382, 0.339242868568382, 0.339242868568382,
            0.319449901768173, 0.319449901768173, 0.319449901768173, 0.319449901768173, 0.319449901768173,
            0.355824915824916, 0.355824915824916, 0.355824915824916, 0.355824915824916, 0.355824915824916, 
            0.343082264957265, 0.343082264957265, 0.343082264957265, 0.343082264957265, 0.343082264957265,
            0.328739896647675, 0.328739896647675, 0.328739896647675, 0.328739896647675, 0.328739896647675,
            0.321470937129300, 0.321470937129300, 0.321470937129300, 0.321470937129300, 0.321470937129300,
            0.329134067099854, 0.329134067099854, 0.329134067099854, 0.329134067099854, 0.329134067099854, 
            0.303929221962009, 0.303929221962009, 0.303929221962009, 0.303929221962009, 0.303929221962009,
            0.318415163880479, 0.318415163880479, 0.318415163880479, 0.318415163880479, 0.318415163880479, 
            0.299444516212376, 0.299444516212376, 0.299444516212376, 0.299444516212376, 0.299444516212376,
            0.343325715822019, 0.343325715822019, 0.343325715822019, 0.343325715822019, 0.343325715822019, 
            0.372169617126390, 0.372169617126390, 0.372169617126390, 0.372169617126390, 0.372169617126390,
            0.370415982484948, 0.370415982484948, 0.370415982484948, 0.370415982484948, 0.370415982484948,
            0.356533513879486, 0.356533513879486, 0.356533513879486, 0.356533513879486, 0.356533513879486,
            0.388973753645327, 0.388973753645327, 0.388973753645327, 0.388973753645327, 0.388973753645327, 
            0.372479078062834, 0.372479078062834, 0.372479078062834, 0.372479078062834, 0.372479078062834,
            0.379030035822541, 0.379030035822541, 0.379030035822541, 0.379030035822541, 0.379030035822541,
            0.407584269662921, 0.407584269662921, 0.407584269662921, 0.407584269662921, 0.407584269662921,
            0.376392901361948, 0.376392901361948, 0.376392901361948, 0.376392901361948, 0.376392901361948, 
            0.317804974338729, 0.317804974338729, 0.317804974338729, 0.317804974338729, 0.317804974338729,
            0.364539393114710, 0.364539393114710, 0.364539393114710, 0.364539393114710, 0.364539393114710,
            0.379058888277380, 0.379058888277380, 0.379058888277380, 0.379058888277380, 0.379058888277380),
      fctr = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
                         1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
                         1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
                         1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
                         1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
                         1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L),
                       .Label = c("a", "b", "c", "d", "e"), class = "factor"),
      y = c(4.04851970360232, -0.102591188819765, 3.73315302756709, 0.340504779534468, 0.237913590714702,
            4.06911664322439, -0.0987598497016705, 5.54001741914177, 0.373135505872404, 0.274375656170733, 
            3.67443913261548, -0.0875837250365816, 3.03232358376749, 0.326682585279794, 
            0.239098860243213, 4.17944244767142, -0.0889017722380819, 3.1663639174688, 
            0.242471128656955, 0.153569356418873, 3.21516073180644, -0.0825729432584231, 
            6.99230459733604, 0.41716036686455, 0.334587423606127, 3.9367867766317, 
            -0.100174510844092, 2.72154053689335, 0.328889772105954, 0.228715261261863, 
            3.99729911731233, -0.100900126928578, 4.53860828306993, 0.366724076035605, 
            0.265823949107028, 4.56693109900323, -0.126308420437427, 5.10762234664757, 
            0.395180555800261, 0.268872135362834, 3.51448237912049, -0.0878426078144829, 
            5.65594606508526, 0.248915416023726, 0.161072808209243, 3.1383099374462, 
            -0.0732066492304829, 4.64242423111922, 0.217790427134848, 0.144583777904365, 
            4.49027171118563, -0.113362808190544, 4.80536464343379, 0.36523834263989, 
            0.251875534449346, 3.45497839018504, -0.0846503583222099, 3.45264289330118, 
            0.286441498422214, 0.201791140100004, 2.09894643097191, -0.0181292647706588, 
            3.15649251734621, 0.487077302298916, 0.468948037528257, 2.88011613789647, 
            0.0332011441108258, 2.17243045829905, 0.473011569737156, 0.506212713847982, 
            3.34598270139375, -0.0610926284972918, 2.64804765938524, 0.50849363186508, 
            0.447401003367788, 2.50787708448308, -0.0538689982930191, 2.38574021348553, 
            0.484438241081951, 0.430569242788932, 3.50259329310981, -0.0851862773159426, 
            3.14484623466867, 0.675500135099749, 0.590313857783806, 5.7188910696372, 
            -0.0954071517814848, 0.583392623483105, 0.624411329255129, 0.529004177473644, 
            2.67827579027081, 0.0460408230771886, 2.52145840963862, 0.625860271515617, 
            0.671901094592805, 3.13901517396219, -0.0693247901648161, 3.2356649048874, 
            0.668874437622921, 0.599549647458105, 2.51959640215471, -0.049164926875836, 
            2.23187740027734, 0.492702963511537, 0.443538036635701, 2.42625504206661, 
            0.0874001285858868, 2.8286583173362, 0.545165351011274, 0.632565479597161, 
            3.24857901035993, -0.0689851948115451, 2.91350545205866, 0.569549019107752, 
            0.500563824296207, 2.87505027703064, -0.0611185132088805, 3.2680427609413, 
            0.552874791715019, 0.491756278506139)), row.names = c(NA, -120L), class = "data.frame")


gtest <- ggplot(data = ex1, aes(x, y)) + 
  
  geom_hline(yintercept = -Inf, size = 0.6) +
  geom_vline(xintercept = -Inf, size = 0.8) +
  
  scale_x_continuous(expand = expansion(mult = c(.06, .1))) +
  scale_y_continuous(expand = expansion(mult = c(.1, .6))) +
  
  geom_point() +
  
  geom_smooth(method = "lm", se = FALSE, formula = my.formula) +
  
  stat_poly_eq(formula = my.formula,
               output.type = "expression", parse = TRUE,
               aes(label = ifelse(stat(p.value) < 0.05,
                                  paste("bold(R)^2 == ", round(stat(r.squared), digits = 3)), ## at least shows the r.squared value, but I can't get the rest bold
                                  ifelse(stat(p.value) < 0.1,
                                         paste("plain(R)^2 == ", round(stat(r.squared), digits = 3)), ##this contains plain(R) because the default is italic, which I don't want
                                         ""))),
               size = 3,
               label.x = .1,
               label.y = .9) +

  stat_poly_eq(formula = my.formula,
               output.type = "expression", parse = TRUE,
               aes(label = ifelse(stat(p.value) < 0.001,
                                  paste("italic(P) < 0.001"),
                                  ifelse(stat(p.value) < 0.05,
                                         paste("bolditalic(P) == ", round(stat(p.value), digits = 3)),  ##at least shows the p.value, but I can't get the rest bold
                                         paste("italic(P) == ", round(stat(p.value), digits = 3))))),
               size = 3,
               label.x = .1,
               label.y = .76) +
  

theme(axis.title.x = element_text(colour = "black", face = "bold", size = 14, margin = margin(10,0,0,0)),
      axis.title.y = element_blank(),
      legend.key = element_blank(),
      legend.title = element_blank(),
      plot.margin = unit(c(0.2,0.4,.2,.2), "cm"),
      plot.title = element_text(colour = "black", face = "bold", size = 16, hjust = 0.5,margin = margin(0,0,10,0)),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.spacing.x = unit(2, "pt"),
      panel.spacing.y = unit(4, "pt"),
      panel.background = element_blank(),
      strip.placement = "outside",
      strip.text.x = element_text(colour = "black", face = "bold", size = 12, margin = margin(0,0,6,0)),
      strip.text.y = element_text(colour = "black", face = "bold", size = 12, margin = margin(0,3,0,0))) + 
  
  facet_grid(fctr ~ time, scales = "free", switch = "y")

gtest

我假设paste()inaes(label = ...)不是 basepaste而是 plotmath paste,因为output.type = "expression"andparse = TRUEstat_poly_eq函数调用中。此外,我已经看到许多使用bquote(), mtext(),substitute()等而不是 的示例paste(),但我无法让他们做我想做的事。另外,我认为这与 ggpmisc 包没有任何关系,而是我不了解如何使用 plotmath 表达式格式化具有可变变量的字符串。

这是我用上面的代码生成的。很难看到,但 R 和 P 在面板 (early, d) 和 (early, e) 中以粗体显示,它们应该是: 只有 R 和 P 格式化

这是我想看到的,整行都是粗体: 整行加粗

任何帮助将不胜感激!

4

1 回答 1

1

bolditalic()bold()像函数一样工作,将格式应用于它们的参数。所以,下面的代码应该做你想做的。我没有在您的示例中对此进行测试,因为它没有按原样运行。

paste("bolditalic(P) == bold(\"", round(stat(p.value), digits = 3), "\")")

我们需要将数字括在引号中bold()以应用于它。这些引号需要进行转义,\"以便将它们识别为字符串的一部分。R 的表达式使得数字和希腊符号永远不会被排版为粗体。因此,通过在解析文本时包含嵌入的引号,数字被视为字符串而不是数字常量。所以这里的关键问题是如何在 R plotmath 表达式中将数字格式化为粗体。

于 2021-06-28T19:21:54.957 回答