2015-05-29 78 views
2

我想要在具有不同参数alpha的同一个图上绘制三条曲线。传说中的希腊字母R

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,阿尔法= 2,阿尔法= 3.如何让正确的吗? enter image description here

回答

6

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

是它可能会更自动地做到这一点 办法?我的真实情节有更多的线条,我有很多这些情节。 –

+0

我想我已经在上面的编辑中解决了它。 –

+1

我更喜欢'legend(“topright”,legend = as.expression(lapply(1:3,function(x)bquote(alpha ==。(x)))),lty = 1:3)' – MrFlick

相关问题