2017-06-06 126 views
0

我有一個矩陣R,我可以使用matplot進行繪圖,但很難自定義繪圖。我想繪製使用R軟件包ggplot,但它不會使用矩陣。我不確定該矩陣需要什麼樣的轉換才能使數據與ggplot一起工作。如何轉換矩陣,以便我可以使用ggplot

感謝您的任何幫助。

+0

你可以強制與as.data.frame一個數據幀,然後用colnames參數添加列名。 – johnckane

+0

您是否嘗試過使用as.data.frame()函數進行轉換? –

+2

一個可重複的例子將有很大的幫助。你可以嘗試(1)'as.data.frame.table()'(2)'reshape2 :: melt()' –

回答

1

您需要將矩陣轉換爲數據幀

mat = cbind(index = seq(1:10), price=7+rnorm(10)) 
    df = as.data.frame(mat) 
    library(ggplot2) 
    ggplot(df) + geom_line(aes(x = index, y = price)) 
相關問題