从R中的矩阵绘制列 [英] Plotting columns from a matrix in R
问题描述
我从模拟中创建了一个值矩阵,该矩阵存储在20x7矩阵中(在7列数字中有20个观察值;该矩阵称为输出).列是从模拟输出的.
I've created a matrix of values from a simulation that is stored in 20x7 matrix (20 observations across 7 columns of numbers; the matrix is called output). The columns are output from a simulation.
运行模拟后,我添加了列名:
After running the simulation, I included column names:
colnames(output) <- c('level', 'value1','value2','value3',
'value4','value5','value6')
并且矩阵看起来很干净.观察时:
And the matrix looks nice and clean. when observing:
output
有没有一种方法可以从矩阵中绘制这些列?我已经尝试了下面的代码(和其他变体),但是它不起作用.
Is there a way to plot these columns from the matrix? I've tried the code below (and other variants), but it will not work.
plot(level$output, value1$output)
谢谢!
推荐答案
要为矩阵建立索引,请使用output[,'level']
,即任意行,'级别'列".
To index the matrix you use output[,'level']
, i.e. "any row, the 'level' column".
plot(output[,'level'],output[,'value1'])
出于您的兴趣,您还可以从矩阵制作数据框并进行如下绘制:
For your interest, you could also make a data frame from your matrix and plot like so:
df <- data.frame(output)
plot(value1 ~ level,df)
如果您对output
所做的全部工作都是不值得做的,但是如果您正在R中对output
进行其他类型的分析,则数据框可能会很方便(然后您可以引用output$value1
,而使用矩阵则必须执行output[,'level']
).
Not worth doing if all you are doing with output
is plotting, but if you are doing other sorts of analysis on output
in R, a dataframe can be handy (and then you can refer to columns like output$level
, output$value1
whereas with a matrix you have to do output[,'level']
).
这篇关于从R中的矩阵绘制列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!