2016-11-21 26 views
-4

在Matlab中,我有以下行繪製:如何在R中獲得類似的繪圖輸出?

plot(x,y,'o',xx,YFIT,'-') 

輸出是這樣的: enter image description here

什麼是去語法的R繪製的最佳方式?第一次在R中進行繪圖工作,所以我在尋求幫助。我可以進一步解釋我的變量,但因爲我只是想翻譯語法,所以我不確定是否需要它。

回答

1

嘗試這樣:

matplot(xx,yy,type="l") 
matlines (xx, YFIT, type = "l", lty = 1:ncol(yy), col = 1:ncol(yy)) 
0

下面我給出了使用ggplot做你想做什麼的一個微小的例子。當然,您不想按照示例中所示準備數據。我建議使用包'reshape2'中的'熔化'功能將數據從矩陣格式轉換爲ggplot預期的長格式。

a.df <- data.frame(
    X=c(0,0.5,1.0), 
    Y=c(0,0.25,1.0), 
    ID = c("A","A","A") 
) 

b.df <- data.frame(
    X=c(0,0.5,1.0), 
    Y=c(0,0.5,1.0), 
    ID = c("B","B","B") 
) 

c.df <- data.frame(
    X=c(0,0.5,1.0), 
    Y=c(0,0.75,1.0), 
    ID = c("C","C","C") 
) 

display.df <- rbind(a.df, b.df, c.df) 

ggplot(display.df, aes(X, Y, color=ID)) + geom_line()