【发布时间】:2021-08-14 23:09:37
【问题描述】:
我想在 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?
【问题讨论】:
-
使用 bquote。我不确定您是否知道在 plotmath 表达式中粘贴不是通常的粘贴功能。