简体   繁体   中英

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!

To index the matrix you use output[,'level'] , ie "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'] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM