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

问题描述

我想在 ggplot2 绘图的 PHP Warning: PHP Startup: Unable to create temporary file,Check permissions in temporary files directory. in UnkNown on line 0 PHP Warning: PHP Startup: POST data can't be buffered; all data discarded in UnkNown on line 0 层中使用下标和数字。该数字将来自索引数据帧,以便我可以在自定义函数中以灵活的方式使用它,但这里有一个更简单的可重现示例:

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),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,size = 1.5,shape = 15) + geom_text(data = temp,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],"= ",digits = 3))) 命令包装在 formatC 中,但我无法使其正常工作。我还根据以下链接将整个表达式包装在 eval()parse() 中。在 substitute() 命令中生成下标和格式化数字时我缺少什么?

我看过的相关帖子: 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,size = 1.5,shape = 15) +
  geom_text(data = temp,label = sprintf("L1 - L[tot] == '%.3e'",temp_num),nudge_x = -0.45,show.legend = FALSE,parse = TRUE)

resulting plot showing the correct text label