2017-06-06 121 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)) 
相关问题