R中传说中的希腊字母
Greek letters in legend in R
我想在同一个图上用不同的参数 alpha 绘制三个曲线。
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright", legend = expression(paste(alpha, " = ", c(1, 2, 3))), lty = 1:3)
在图例中,我想要 alpha = 1、alpha = 2、alpha = 3 的三行。如何使其正确?
更好的循环答案来自 here 和 user20650。
用sapply解决
expression
函数非常棘手,但结合 substitute
您可以使用 sapply
循环:
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright",
legend = sapply(1:3, function(x) as.expression(substitute(alpha == B,
list(B = as.name(x))))),
lty = 1:3)
简单修复
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright", legend = c(expression(paste(alpha, " = ", 1)),
expression(paste(alpha, " = ", 2)),
expression(paste(alpha, " = ", 3))), lty = 1:3)
我想在同一个图上用不同的参数 alpha 绘制三个曲线。
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright", legend = expression(paste(alpha, " = ", c(1, 2, 3))), lty = 1:3)
在图例中,我想要 alpha = 1、alpha = 2、alpha = 3 的三行。如何使其正确?
更好的循环答案来自 here 和 user20650。
用sapply解决
expression
函数非常棘手,但结合 substitute
您可以使用 sapply
循环:
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright",
legend = sapply(1:3, function(x) as.expression(substitute(alpha == B,
list(B = as.name(x))))),
lty = 1:3)
简单修复
curve(sin(x), from = 1, to = 3, lty = 1, ylim = c(-1, 1))
curve(sin(2 * x), add = TRUE, lty = 2)
curve(sin(3 * x), add = TRUE, lty = 3)
legend("topright", legend = c(expression(paste(alpha, " = ", 1)),
expression(paste(alpha, " = ", 2)),
expression(paste(alpha, " = ", 3))), lty = 1:3)