2012-05-19 34 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

相關問題