Plotting columns from a matrix in R

安稳与你 提交于 2020-01-15 18:48:47

问题


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)

thanks!


回答1:


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)

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



来源:https://stackoverflow.com/questions/8919712/plotting-columns-from-a-matrix-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!