2015-05-29 126 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

相關問題