【问题标题】:Greek letters in legend in RR中传说中的希腊字母
【发布时间】:2015-08-12 22:29:11
【问题描述】:

我想在同一个图上用不同的参数 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)

在图例中,我想要三行 alplha = 1, alpha = 2, alpha = 3。如何使它正确?

【问题讨论】:

    标签: r expression legend


    【解决方案1】:

    更好的循环答案来自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)
    

    【讨论】:

    • 是否可以以更自动的方式执行此操作?我的真实情节有更多的线条,我有很多这样的情节。
    • 我更喜欢legend("topright", legend = as.expression(lapply(1:3, function(x) bquote(alpha==.(x)))), lty = 1:3)
    猜你喜欢
    • 1970-01-01
    • 2011-08-28
    • 2014-11-02
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多