title: R: circlize circos plot - how to plot unconnected areas between sectors with minimal overlap
body: <p>I have a data frame with common features between 4 groups of patients and cell types. I have a lot of different features, but the shared ones (present in more than 1 group) are just a few.</p>
  
  <p>I want to make a circos plot that reflects the few connections between shared features across groups of patients and cell types, while giving an idea of how many unshared features there are in each group.</p>
  
  <p>The way I think of it, it should be a plot with 4 sectors (one for each group of patient and cell type) with a few connections between them. Each sector size should reflect the total number of features in the group, and most of this area should not be connected to other groups, but empty.</p>
  
  <p>This is what I have so far, but I do not want sectors dedicated to each feature, only to each group of patient and cell type.</p>
  
  <p>MWE:</p>
  
  <pre><code>library(circlize)
  
  patients &lt;- c(rep("patient1",20), rep("patient2",10))
  cell.types &lt;- c(rep("cell1",12), rep("cell2",8),rep("cell1",6), rep("cell2",4))
  features &lt;- c(paste("feature",1:12,sep="_"), paste("feature",9:16,sep="_"), paste("feature",c(1,2,9,10,17,18),sep="_"), paste("feature",c(1,18,19,20),sep="_"))
  dat &lt;- data.frame(patient=patients, cell.type=cell.types, feature=features)
  dat
  dat &lt;- with(dat, table(paste(patient,cell.type,sep='|'), feature))
  dat
  
  chordDiagram(as.data.frame(dat), transparency = 0.5)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/GNYkq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GNYkq.png" alt="fig1"></a></p>
  
  <p><strong>EDIT!!</strong></p>
  
  <p>What @m-dz shows in his answer is actually the format I am looking for, 4 sectors for the 4 different patient/cell.type combinations, showing just the connections, while the non-connected features, though not shown, should account for the size of the sector.</p>
  
  <p>However, I am realizing I have a more complicated scenario than the one in the MWE above.</p>
  
  <p>A feature is considered to appear in 2 patient/cell.type groups, not only when it is <em>identical</em> in the 2 groups, but also when it is <em>similar</em>... (sequence identity above a threshold). This way, I have redundancies...</p>
  
  <p>Feature A in patient1-cell1 can be connected to feature A in patient2-cell1, but also to feature B... Feature A should only be counted once (unique counts) for patient1-cell1, and expand to 2 different features in patient2-cell1.</p>
  
  <p>See below an example of how my actual data looks like more precisely, and see if working with this example we can get the final circos plot! Thanks!!</p>
  
  <pre><code>##MWE
  #NON OVERLAPPING SETS!
  
  #1: non-shared features
  nonshared &lt;- data.frame(patient=c(rep("pat1",20), rep("pat2",10)), cell.type=c(rep("cell1",12), rep("cell2",8),rep("cell1",6), rep("cell2",4)), feature=paste("a",1:30,sep=''))
  nonshared
  
  #2: features shared between cell types within same patient
  sharedcells &lt;- data.frame(patient=c(rep("pat1",3), rep("pat2",4)), cell.types=c(rep("cell1||cell2",3),rep("cell1||cell2",4)), features=c("b1||b1","b1||b1","b1||b1","b2||b2","b3||b3","b4||b4","b4||b5"))
  sharedcells
  
  #3: features shared between patients within same cell types
  sharedpats &lt;- data.frame(patients=c(rep("pat1||pat2",2), rep("pat1||pat2",6)), cell.type=c(rep("cell1",2),rep("cell2",6)), features=c("c1||c1","c2||c1","c3||c3","c3||c4","c3||c5","c6||c5","c7||c7","c8||c8"))
  sharedpats
  
  #4: features shared between patients and cell types
  #4.1: shared across pat1-cell1, pat1-cell2, pat2-cell1, pat2-cell2
  sharedall1 &lt;- data.frame(both=c(rep("pat1-cell1||pat1-cell2||pat2-cell1||pat2-cell2",4)), features=c("d1||d1||d1||d1","d2||d2||d2||d3","d4||d4||d3||d3","d5||d5||d5||d5"))
  #4.2: shared across pat1-cell1, pat1-cell2, pat2-cell1
  sharedall2 &lt;- data.frame(both=c(rep("pat1-cell1||pat1-cell2||pat2-cell1",2)), features=c("d6||d6||d6","d7||d7||d7"))
  #4.3: shared across pat1-cell1, pat1-cell2, pat2-cell2
  sharedall3 &lt;- data.frame(both="pat1-cell1||pat1-cell2||pat2-cell2", features="d8||d8||d9")
  #4.4: shared across pat1-cell1, pat2-cell1, pat2-cell2
  sharedall4 &lt;- data.frame(both="pat1-cell1||pat2-cell1||pat2-cell2", features="d10||d10||d9")
  #4.5: shared across pat1-cell2, pat2-cell1, pat2-cell2
  sharedall5 &lt;- data.frame(both=c(rep("pat1-cell2||pat2-cell1||pat2-cell2",3)), features=c("d11||d11||d11","d12||d13||d13","d12||d14||d14"))
  #4.6: shared across pat1-cell1, pat2-cell2
  sharedall6 &lt;- data.frame()
  #4.7: shared across pat1-cell2, pat2-cell1
  sharedall7 &lt;- data.frame(both=c(rep("pat1-cell2||pat2-cell1",2)), features=c("d15||d16","d17||d17"))
  
  sharedall &lt;- rbind(sharedall1, sharedall2, sharedall3, sharedall4, sharedall5, sharedall6, sharedall7)
  sharedall
  #you see there might be overlaps between the different subsets of sharedall, but not between sharedall, sharedparts, sharedcells, and nonshared
  
  #I NEED A CIRCOS PLOT THAT SHOWS ALL THE CONNECTIONS. THE NON-CONNECTED (nonshared) FEATURES SHOULD NOT BE SHOWN, BUT THE SHOULD COUNT TO THE SIZE OF THE SECTOR (CORRESPONDING TO A PATIENT-CELL COMBINATION)
  
  #THE FEATURES SHOULD BE COUNT UNIQUELY, SO IF THERE ARE ENTRIES LIKE:
  #3 pat1||pat2     cell2   c3||c3
  #4 pat1||pat2     cell2   c3||c4
  #5 pat1||pat2     cell2   c3||c5
  #THE FEATURE c3 SHOULD BE COUNT ONCE FOR pat1, AND EXPAND TO 3 DIFFERENT FEATURES IN pat2
  </code></pre>