Captain Murphy
Captain Murphy

Reputation: 865

Plotting columns from a matrix in R

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!

Upvotes: 1

Views: 32277

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56925

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

Upvotes: 5

Related Questions