-1

I have a list called: list_plot

list_plot=list(list(a = c(2, 3, 4, 5), b = c(3, 4, 5, 5), c = c(3, 7, 5, 
5), d = c(3, 4, 9, 5), e = c(3, 4, 5, 9), f = c(3, 4, 1, 9), 
    g = c(3, 1, 5, 9), h = c(3, 3, 5, 9), i = c(3, 17, 3, 9), 
    j = c(3, 17, 3, 9)), list(a = c(2, 3, 4, 5), b = c(3, 4, 
5, 5), c = c(3, 7, 5, 5), d = c(3, 4, 9, 5), e = c(3, 4, 5, 9
), f = c(3, 4, 1, 9), g = c(3, 1, 5, 9), h = c(3, 3, 5, 9), i = c(3, 
17, 3, 9), j = c(3, 17, 3, 9)), list(a = c(2, 3, 4, 5), b = c(3, 
4, 5, 5), c = c(3, 7, 5, 5), d = c(3, 4, 9, 5), e = c(3, 4, 5, 
9), f = c(3, 4, 1, 9), g = c(3, 1, 5, 9), h = c(3, 3, 5, 9), 
    i = c(3, 17, 3, 9), j = c(3, 17, 3, 9)), list(a = c(2, 3, 
4, 5), b = c(3, 4, 5, 5), c = c(3, 7, 5, 5), d = c(3, 4, 9, 5
), e = c(3, 4, 5, 9), f = c(3, 4, 1, 9), g = c(3, 1, 5, 9), h = c(3, 
3, 5, 9), i = c(3, 17, 3, 9), j = c(3, 17, 3, 9)), list(a = c(2, 
3, 4, 5), b = c(3, 4, 5, 5), c = c(3, 7, 5, 5), d = c(3, 4, 9, 
5), e = c(3, 4, 5, 9), f = c(3, 4, 1, 9), g = c(3, 1, 5, 9), 
    h = c(3, 3, 5, 9), i = c(3, 17, 3, 9), j = c(3, 17, 3, 9)))

In this list_plot [[i]], i goes from 1 to 5. This list has 10 entries, j = 10 (list_plot [[i]] [j]).

So, for me to get the input one I do this: list_plot [[i]] [1]

Each list_plot [[i]] [j] is a series of numbers that I plot a chart. Here is the code for this graphic:

 for (i in 1: 5) {
             for (j in 1:10){
x11 ()
 par (mfrow = c (3.2))
    plot.ts (list_plot [[i]] [j])
          }

 }

It is showing an error. I want that when i = 1, it goes through allj, which goes from 1 to 10. Then, it can start i = 2, traversing all j, which again goes from 1 to 10. That is, when i = 1 the priority is now to finish j. When i = 2 the priority is to end allj``s and so on.

Any help?

  • 1
    Can you provide some example data? Perhaps with `dput(list_plot[[1]])` – DanY Aug 23 '18 at 22:22
  • 1
    Can you please include minimal representative sample data for `list_plot` (use e.g. `dput` and edit your post to include the output). Also by `mfrow = c(3.2)` you probably mean `mfrow = c(3, 2)`, i.e. a 3 by 2 grid layout? – Maurits Evers Aug 23 '18 at 22:22
  • @DanY I updated. –  Aug 23 '18 at 23:18
  • @MauritsEvers I edited –  Aug 23 '18 at 23:19
  • 1
    Possible duplicate of [Plot two graphs in same plot in R](https://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) –  Aug 24 '18 at 00:01

1 Answers1

0

You need extra brackets around j. Change the plot call to:

plot.ts(list_plot[[i]][[j]])

The reason is that [ and [[ return objects of a different class (the double brackets will simplify to a vector whereas the single bracket keep the subset as a list):

class(list_plot[[1]][1])
"list"

class(list_plot[[1]][[1]])
"numeric"
DanY
  • 5,920
  • 1
  • 13
  • 33
  • but what about the command `x11 ()` `par (mfrow = c (3,2))`? I will only have one graph. How can I inlude it in the `for`? Its suppose to give me 50 graphics (10x5=`j` x `i`). –  Aug 23 '18 at 23:27
  • I did the `par(mfrow=c(3,2))`. And I only have one plot –  Aug 23 '18 at 23:37
  • With x11() I want al the plots that the loop will give me. I am using Rstudio –  Aug 23 '18 at 23:42