title: R circos plot calculating percent of total for each category
body: <p>My data has two variables, TRV and TRJ, and I am seeing how often they match with each other. I would like to place on a circos figure what percent each variable shows up in total on the outer layer. It should add up to 200%, 100% for TRV, 100% for TRJ.</p>
  <pre><code>library(circlize)
  library(plyr)
  df &lt;- structure(list(TRV = structure(c(1L, 1L, 1L, 2L, 3L, 3L, 3L, 
  3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c(&quot;TRAV29/DV5&quot;, &quot;TRAV36/DV7&quot;, 
  &quot;TRDV1&quot;, &quot;TRDV2&quot;, &quot;TRDV3&quot;, &quot;TRGV8&quot;, &quot;TRGV9&quot;), class = &quot;factor&quot;), 
      TRJ = structure(c(64L, 65L, 67L, 64L, 64L, 65L, 66L, 67L, 
      64L, 65L, 66L, 64L, 65L, 66L), .Label = c(&quot;&quot;, &quot;mTRAJ22&quot;, 
      &quot;mTRAJ30&quot;, &quot;mTRAJ34&quot;, &quot;mTRAJ37&quot;, &quot;mTRAJ45&quot;, &quot;mTRAJ49&quot;, &quot;mTRBJ1-1&quot;, 
      &quot;mTRBJ2-5&quot;, &quot;mTRDJ1&quot;, &quot;mTRDJ2&quot;, &quot;mTRGJ1&quot;, &quot;mTRGJ4&quot;, &quot;TRAJ10&quot;, 
      &quot;TRAJ15&quot;, &quot;TRAJ16&quot;, &quot;TRAJ19&quot;, &quot;TRAJ2&quot;, &quot;TRAJ20&quot;, &quot;TRAJ21&quot;, 
      &quot;TRAJ22&quot;, &quot;TRAJ23&quot;, &quot;TRAJ24&quot;, &quot;TRAJ26&quot;, &quot;TRAJ27&quot;, &quot;TRAJ30&quot;, 
      &quot;TRAJ32&quot;, &quot;TRAJ34&quot;, &quot;TRAJ36&quot;, &quot;TRAJ37&quot;, &quot;TRAJ38&quot;, &quot;TRAJ39&quot;, 
      &quot;TRAJ40&quot;, &quot;TRAJ41&quot;, &quot;TRAJ42&quot;, &quot;TRAJ43&quot;, &quot;TRAJ44&quot;, &quot;TRAJ45&quot;, 
      &quot;TRAJ49&quot;, &quot;TRAJ5&quot;, &quot;TRAJ50&quot;, &quot;TRAJ52&quot;, &quot;TRAJ53&quot;, &quot;TRAJ54&quot;, 
      &quot;TRAJ56&quot;, &quot;TRAJ57&quot;, &quot;TRAJ58&quot;, &quot;TRAJ6&quot;, &quot;TRAJ7&quot;, &quot;TRAJ8&quot;, 
      &quot;TRBJ1-1&quot;, &quot;TRBJ1-2&quot;, &quot;TRBJ1-3&quot;, &quot;TRBJ1-4&quot;, &quot;TRBJ1-5&quot;, &quot;TRBJ1-6&quot;, 
      &quot;TRBJ2-1&quot;, &quot;TRBJ2-2&quot;, &quot;TRBJ2-3&quot;, &quot;TRBJ2-4&quot;, &quot;TRBJ2-5&quot;, &quot;TRBJ2-6&quot;, 
      &quot;TRBJ2-7&quot;, &quot;TRDJ1&quot;, &quot;TRDJ2&quot;, &quot;TRDJ3&quot;, &quot;TRDJ4&quot;, &quot;TRGJ1&quot;, &quot;TRGJ2&quot;, 
      &quot;TRGJP&quot;, &quot;TRGJP1&quot;, &quot;TRGJP2&quot;), class = &quot;factor&quot;), freq = c(387L, 
      3L, 1L, 1L, 3533L, 445L, 132L, 55L, 563L, 15L, 5L, 830L, 
      4L, 72L)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
  10L, 11L, 12L, 13L, 14L, 15L), class = &quot;data.frame&quot;)
  
  grid.col = c(`TRAV29/DV5` = &quot;red&quot;, TRDV3 = &quot;green&quot;, TRDV2 = &quot;blue&quot;, TRDV1 = &quot;purple&quot;, `TRAV36/DV7` = &quot;pink&quot;, 
               TRDJ4 = &quot;orange&quot;, TRDJ1 = &quot;palegreen&quot;, TRDJ2 = &quot;lightsteelblue&quot;, TRDJ3 = &quot;thistle&quot;, TRGJP = &quot;yellow&quot;, TRGJ2 = &quot;grey&quot;, TRGJP2 = &quot;brown&quot;, TRGJ1 = &quot;lightpink&quot;)
  
  circ_plot &lt;- function(df){
    circos.par(canvas.xlim=c(-1.5,1.5),canvas.ylim=c(-1.5,1.5))
    chordDiagram(df, annotationTrack = &quot;grid&quot;,
                 grid.col = grid.col,
                 link.lwd = matrix(1, nrow = nrow(df), ncol = ncol(df)), 
                 link.border = &quot;black&quot;)
    for(si in get.all.sector.index()) {
      xlim = get.cell.meta.data(&quot;xlim&quot;, sector.index = si, track.index = 1)
      ylim = get.cell.meta.data(&quot;ylim&quot;, sector.index = si, track.index = 1)
      circos.text(mean(xlim),ylim[1], si, sector.index = si, track.index = 1, 
                  facing = &quot;clockwise&quot;, 
                  cex=0.8, 
                  adj=c(-1,0),
                  niceFacing = TRUE)
      circos.axis(h = 0,
                  major.at = c(0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5) ,
                  labels.cex = 0.2,labels.facing = &quot;inside&quot;, 
                  sector.index = si, track.index = 1)
    }
    # Restart circular layout parameters
    circos.clear()
  }
  
  circ_plot(df)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/xZeQt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xZeQt.png" alt="enter image description here" /></a></p>