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

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

问题描述

matplot() 可以轻松地按列绘制矩阵/二维数组(也适用于数据框):

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

使用 ggplot2 是否有类似的东西,或者 ggplot2 是否需要首先将数据融合到数据帧中?

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

另外,有没有一种方法可以使用单独的向量(length=ncol(a))对矩阵列的子集进行任意颜色/样式设置?

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

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

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