如何变换矩阵以便我可以使用ggplot进行绘图

时间:2021-04-12 23:44:46

I have a matrix in R that I can plot using matplot however it is hard to customize the plot. I would like to plot using the R package ggplot however it will not work using a matrix. I am not sure what transformations are required of the matrix to allow the data to work with ggplot.

我在R中有一个矩阵,我可以使用matplot进行绘图,但很难自定义绘图。我想使用R包ggplot进行绘图,但是使用矩阵不起作用。我不确定矩阵需要哪些转换才能使数据与ggplot一起使用。

Thanks for any help.

谢谢你的帮助。

1 个解决方案

#1


1  

you need to convert the matrix to a data frame

您需要将矩阵转换为数据框

    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))

#1


1  

you need to convert the matrix to a data frame

您需要将矩阵转换为数据框

    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))