2012-05-19 35 views
4

我想拿出一个不涉及使用其他软件包如ggplot的解决方案。虽然绘制多条线非常简单,但我还没有想出将不同的线应用不同的参数值(例如,不同的颜色)的方法。下面的代码(和结果图)是我的尝试,显然没有做我想做的事情。我也不想使用循环,因为我试图让我的脚本尽可能简单。将行()应用于数据框/矩阵的列;每行不同的颜色

df = cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10))) 
plot(0, xlim = c(1,10), ylim=range(df), type="n") 
apply(df, 2, lines, type="b", col = c("red", "blue", "black")) 

Plot generated using the code above 我真正想要的是一个阴谋象下面这样:

plot(0, xlim = c(1,10), ylim=range(df), type="n") 
color = c("red","blue","black") 
for(i in 1:3){ 
    lines(1:10, df[,i], type = "b", col=color[i]) 
} 

The desired plot 预先感谢您!

回答

4

尝试matplot()

df <- cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10))) 
matplot(df, type="b", lty=1, pch=1, col=c("blue", "red", "black")) 

enter image description here

+0

真棒!它是否允许与plot()/ lines()相同的自定义级别?无论如何,我会玩这个功能一点。谢谢! – Alex

相关问题