Home > OS >  Elementwise plotting of a list of vectors
Elementwise plotting of a list of vectors

Time:08-24

I want to plot corresponding elements of my_list as a line. For example, first elements of vectors x, y, and z become the first line of the plot.

The second elements of vectors x, y, and z become one line of the plot and so on.

I have come up with a long solution below (desired plot). But I wonder if there is a more efficient (automated like a loop) solution to achieve the same plot?

my_list <- list(x=c(.1,.2,.7), y=c(.3,.5,.8), z=c(.4,.6,.9))

plot(x = seq_len(length(my_list)), y = c(.1,.3,.4), type = "l", ylim = range(my_list))

lines(x = seq_len(length(my_list)), y = c(.2,.5,.6))

lines(x = seq_len(length(my_list)), y = c(.7,.8,.9))

CodePudding user response:

matplot(t(as.data.frame(my_list)), type = 'l')

enter image description here

  • Related