在 R 的绘图标签中一起使用 paste() 和 formatC()

Using paste() and formatC() together in plot label in R

我想在 ggplot2 图的 geom_text() 层中使用下标和数字。该数字将来自索引数据框,以便我可以在自定义函数中以灵活的方式使用它,但这是一个更简单的可重现示例:

temp_line <- structure(list(x = c(670.892544634835, 670.94367492675, 670.994805218665, 
671.04593551058, 671.097065802495, 671.14819609441, 671.199326386325, 
671.25045667824, 671.301586970155, 671.35271726207, 671.403847553985, 
671.4549778459, 671.506108137815, 671.55723842973, 671.608368721645, 
671.65949901356, 671.710629305475, 671.76175959739, 671.812889889305, 
671.86402018122, 671.915150473135, 671.96628076505, 672.017411056965, 
672.06854134888, 672.119671640795, 672.17080193271, 672.221932224625, 
672.27306251654, 672.324192808455, 672.37532310037, 672.426453392285, 
672.4775836842, 672.528713976115, 672.57984426803, 672.630974559945, 
672.68210485186), y = c(0.985240554809607, 0.982213133032921, 
0.986693753084667, 0.987366974516756, 0.987636649598203, 0.982056033377924, 
0.980484643703154, 0.983029322815495, 0.988833379596215, 0.983723421126925, 
0.984861430761356, 0.975375941059582, 0.973793277004997, 0.974896425651856, 
0.976215113076567, 0.980190883847887, 0.97234555785344, 0.976564419204803, 
0.971411825270084, 0.958092987035761, 0.948933680537213, 0.944433555718954, 
0.955207036644352, 0.966236032356096, 0.97461001903587, 0.982502838158965, 
0.977806030386617, 0.980102357820749, 0.971970360584438, 0.972510182292617, 
0.971868273995148, 0.986332867356837, 0.992939055558675, 1, 0.990384208442822, 
0.987140151281311)), class = "data.frame", row.names = c(NA, 
-36L))

temp <- structure(list(x = 671.96628076505, y = 0.943555524955473), class = "data.frame", row.names = "window_mins")

temp_num <- c(window_mins = 0.000878030763480352)


# Attempt at plotting equation using paste()
  ggplot() +
    geom_line(data = temp_line, 
              aes(x = x, y = y), alpha = 0.85, size = 0.6) +
    geom_point(data = temp,
               aes(x = x, y = y), alpha = 0.85, size = 1.5, shape = 15) +
    geom_text(data = temp,
              aes(x = x, y = y),
              label = paste("L1 - L[tot] = ", 
                                       formatC(temp_num, 
                                               format = "e", digits = 3)),
              nudge_x = -0.45,
              show.legend = FALSE)

挑战是以对下标和格式化数字都有效的方式使用 paste()。我尝试同时使用 expression()paste()。这创建了正确的下标,但不幸的是打印了 formatC() 的命令和参数,而不是计算生成数字:

label = expression(paste("L1 - ", L[tot], "= ", 
                                       formatC(temp_num, 
                                               format = "e", digits = 3)))

我尝试了上面的方法,但是将 formatC 命令包装在 eval() 中,但我无法让它工作。我还根据下面的链接尝试将整个表达式包装在 parse()substitute() 中。在 paste() 命令中生成下标和格式化数字我缺少什么?

我看过的相关帖子: Combining paste() and expression() functions in plot labels

https://community.rstudio.com/t/use-bquote-in-ggplot2-for-subscript-text/40882

R - how to combine expression(), paste() and formatC() commands for one legend element?

好的,这出奇地棘手。

首先,你需要学习help("plotmath")。然后,我将传递一个字符串并让 ggplot2 解析它。如果我们这样做,方程的 RHS 应指定为字符串,否则将在 plotmath 中应用标准打印格式。为方便起见,我更喜欢 sprintf,但如果您坚持可以使用 pasteformatC.

的组合构造相同的字符串
#note the single quotes within the string
sprintf("L1 - L[tot] == '%.3e'", temp_num)
#[1] "L1 - L[tot] == '8.780e-04'"

ggplot() +
  geom_line(data = temp_line, 
            aes(x = x, y = y), alpha = 0.85, size = 0.6) +
  geom_point(data = temp,
             aes(x = x, y = y), alpha = 0.85, size = 1.5, shape = 15) +
  geom_text(data = temp,
            aes(x = x, y = y),
            label = sprintf("L1 - L[tot] == '%.3e'", temp_num),
            nudge_x = -0.45,
            show.legend = FALSE,
            parse = TRUE)