gpplot2相当于matplot():按列绘制矩阵/数组? [英] ggplot2 equivalent of matplot() : plot a matrix/array by columns?

查看:229
本文介绍了gpplot2相当于matplot():按列绘制矩阵/数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplot()可以很容易地按列绘制一个矩阵/二维数组(也适用于数据框架):

  a<  - 矩阵(rnorm(100),c(10,10))
matplot(a,type ='l')

使用ggplot2有什么相似之处,或者ggplot2要求数据是融化

另外,是否有一种方法可以使用单独的矢量( length = ncol(a))?

解决方案



  library(ggplot2)
a < - matrix(rnorm(100),c(10,10))
sa < - stack(as.data.frame(a))
sa $ x < - rep(seq_len(nrow(a)),ncol(a))
qplot(x ,值,data = sa,group = ind,color = ind,geom =line)


matplot() makes it easy to plot a matrix/two dimensional array by columns (also works on data frames):

a <- matrix (rnorm(100), c(10,10))
matplot(a, type='l')

Is there something similar using ggplot2, or does ggplot2 require data to be melted into a dataframe first?

Also, is there a way to arbitrarily color/style subsets of the matrix columns using a separate vector (of length=ncol(a))?

解决方案

Maybe a little easier for this specific example:

library(ggplot2)
a <- matrix (rnorm(100), c(10,10))
sa <- stack(as.data.frame(a))
sa$x <- rep(seq_len(nrow(a)), ncol(a))
qplot(x, values, data = sa, group = ind, colour = ind, geom = "line")

这篇关于gpplot2相当于matplot():按列绘制矩阵/数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆