title: Optimize a plot by reducing the number of intersections (in R)
body: <p>I have a big data set where each entry has 8 properties.
  When plotting it in a diagram as shown I get a really messy plot. <br>
  Therefore I was planning to calculate the best permutation of the properties (the ones with "...ic") where there are minimum intersection between the lines. I already tried this manually by sorting the properties so that "green" looks as linear as possible, but I wanna know what the solution with minimal intersections is.<br><br>
  So I came to the question if i can count those intersections somehow, or if there is an even easier way to solve my problem. (Maybe change the scale? But in my understanding that shouldn't change anything...)<br><br>
  The plot is generated by the following function:</p>
  
  <pre><code> function(oliveareax, oliveareagroups, oliveareacol=regioncol)
    {
      supline &lt;- trellis.par.get("superpose.line")
      suplineold &lt;- supline
      supline$col = oliveareacol
      trellis.par.set("superpose.line" = supline)
      print(parallelplot(oliveareax,
                         horizontal.axis = FALSE, scales = list(x = list(rot = 90)),
                         groups = olives$region,
                         auto.key=TRUE,
      ) )
      trellis.par.set("superpose.line" = suplineold)
      rm(suplineold, supline)
    }
  </code></pre>
  
  <p>Plot is given below:</p>
  
  <p><a href="https://i.stack.imgur.com/9vbV3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9vbV3.png" alt="enter image description here"></a></p>
  
  <p>EDIT: I wrote two functions to count the number of intersection of the means and to find the permutation for the minimum intersection of the means. The latter one is not working (and very inefficient), maybe someone can help me at least with that? It gives out "1 2 2 2 2 2 2 2", although that shouldn't be possible with my implementation...<br>
  <br></p>
  
  <pre><code>findminintersec &lt;- function(set1, set2, set3){
    ptm &lt;- proc.time()
    idi &lt;- 1:8
    mincount &lt;- .Machine$double.max
    for(i in 1:8){
      idj &lt;- idi[-i]
      for(j in idj){
        idk &lt;- idj[-j]
        for(k in idk){
          idl &lt;- idk[-k]
          for(l in idl){
            idm &lt;- idl[-l]
            for(m in idm){
              idn &lt;- idm[-m]
              for(n in idn){
                ido &lt;- idn[-n]
                for(o in ido){
                  idp &lt;- ido[-o]
                  for(p in idp){
                    count &lt;- interseccount(set1[c(i,j,k,l,m,n,o,p)], set2[c(i,j,k,l,m,n,o,p)], set3[c(i,j,k,l,m,n,o,p)])
                    if(count &lt; mincount){
                      mincount &lt;- count
                      minperm &lt;- c(i,j,k,l,m,n,o,p)
  
                    }
                  }
                  idp &lt;- 1:8
                }
                ido &lt;- 1:8
              }
              idn &lt;- 1:8
            }
            idm &lt;- 1:8
          }
          idl &lt;- 1:8
        }
        idk &lt;- 1:8
      }
      idj &lt;- 1:8
    }
    #return(mincount)
    print(proc.time() - ptm)
    return(minperm)
  }
  </code></pre>