<p>It's possible that <code>upset</code> is using factor levels for ordering, which is the default approach for ggplot2. If this is the case, then it can be fixed by converting the variable to a factor, and using an explicit level definition:</p>
<pre><code>sets &lt;- factor(sets, levels=sets)
</code></pre>
<p>For your specific example:</p>
<pre><code>upSets &lt;- c(&quot;Nlab&quot;, &quot;NQLD&quot;, &quot;Ngla&quot;, &quot;Nsyl&quot;, &quot;Ntom&quot;, &quot;Ntab&quot;, &quot;Natt&quot;, &quot;Cann&quot;, &quot;Stub&quot;, &quot;Slyc&quot;, &quot;Vvin&quot;, &quot;Atha&quot;)
upSets &lt;- factor(upSets, levels=upSets)
upset(orthogroups_df, 
   text.scale = c(1.4),
   sets=upSets, 
   order.by = &quot;freq&quot;,
   keep.order=T, 
   intersections = intersections, 
   sets.x.label=&quot;Total number of orthogroups&quot;, 
   mainbar.y.label = &quot;Number of orthogroups&quot;) 
</code></pre>

CC BY-SA 4.0
2022-12-08T19:09:34.870Z
2022-12-14T04:26:49.670Z
2022-12-14T04:26:49.670Z
gringer
<p><em>This question was also asked on <a href="https://www.biostars.org/p/9547684/" rel="nofollow noreferrer">Biostars</a></em></p>
<p>I have tried to use UpsetR to visualise the input file, which can be found <a href="https://cloudstor.aarnet.edu.au/plus/s/OupXC8rF2bDFtRV" rel="nofollow noreferrer">here</a>. How is it possible to make UpsetR accept an order defined in <code>sets=c(&quot;Nlab&quot;, &quot;NQLD&quot;, &quot;Ngla&quot;, &quot;Nsyl&quot;, &quot;Ntom&quot;, &quot;Ntab&quot;, &quot;Natt&quot;, &quot;Cann&quot;, &quot;Stub&quot;, &quot;Slyc&quot;, &quot;Vvin&quot;, &quot;Atha&quot;)</code>?</p>
<p><strong>with keep.order = T</strong>
<a href="https://i.sstatic.net/UemPY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UemPY.png" alt="with keep.order = T" /></a></p>
<p><strong>with keep.order = F</strong>
<a href="https://i.sstatic.net/gkcGe.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gkcGe.png" alt="with keep.order = F" /></a></p>
<pre><code>&gt; library(&quot;UpSetR&quot;)
&gt; orthogroups_df&lt;- read.table(&quot;orthogroups.GeneCount.tsv&quot;,  header=T, stringsAsFactors = F)
&gt; #All species
&gt; selected_species &lt;- colnames(orthogroups_df)[2:(ncol(orthogroups_df) -1)] 
&gt; selected_species
 [1] &quot;Atha&quot; &quot;Cann&quot; &quot;NQLD&quot; &quot;Natt&quot; &quot;Ngla&quot; &quot;Nlab&quot; &quot;Nsyl&quot; &quot;Ntab&quot; &quot;Ntom&quot; &quot;Slyc&quot; &quot;Stub&quot; &quot;Vvin&quot;
&gt; head(orthogroups_df)
  Orthogroup Atha Cann NQLD Natt Ngla Nlab Nsyl Ntab Ntom Slyc Stub Vvin Total
1  OG0000000    0    0  965    0    0    3    0    0    0    0    0    0   968
2  OG0000001    0    1    3    0    0  448    0    0    0    0    0    0   452
3  OG0000002    0    1  313    0    0  120    1    0    1    0    0    0   436
4  OG0000003    0   93   15   21   46   16   33   63   36   25   39   26   413
5  OG0000004    1   42    2   34  109    6    8  154   11    9    4    0   380
6  OG0000005    0    2   61    1   34   44   91   70   43   20    1    0   367
&gt; ncol(orthogroups_df)
[1] 14
&gt; orthogroups_df[orthogroups_df &gt; 0] &lt;- 1
&gt; # we only show intersections of interest ,  
&gt; intersections=list(list(selected_species),
+                    list(&quot;NQLD&quot;, &quot;Ngla&quot;, &quot;Natt&quot;, &quot;Nlab&quot;, &quot;Nsyl&quot;, &quot;Ntab&quot;, &quot;Ntom&quot;),
+                    list(&quot;Stub&quot;, &quot;Slyc&quot;),
+                    list(&quot;Atha&quot;, &quot;Vvin&quot;),
+                    list(&quot;Ntab&quot;, &quot;Nsyl&quot;, &quot;Ntom&quot;),
+                    list(&quot;Nlab&quot;, &quot;NQLD&quot;, &quot;Ngla&quot;), 
+                    list(&quot;Nlab&quot;, &quot;NQLD&quot;, &quot;Nsyl&quot;), 
+                    list(&quot;Nlab&quot;, &quot;Ngla&quot;, &quot;Nsyl&quot;), 
+                    list(&quot;NQLD&quot;, &quot;Nsyl&quot;, &quot;Ngla&quot;))
&gt; upset(orthogroups_df, 
+       text.scale = c(1.4),
+       sets=c(&quot;Nlab&quot;, &quot;NQLD&quot;, &quot;Ngla&quot;, &quot;Nsyl&quot;, &quot;Ntom&quot;, &quot;Ntab&quot;, &quot;Natt&quot;, &quot;Cann&quot;, &quot;Stub&quot;, &quot;Slyc&quot;, &quot;Vvin&quot;, &quot;Atha&quot;), 
+       order.by = &quot;freq&quot;,
+       keep.order=T, 
+       intersections = intersections, 
+       sets.x.label=&quot;Total number of orthogroups&quot;, 
+       mainbar.y.label = &quot;Number of orthogroups&quot;) 
</code></pre>

<r><ggplot2><upsetr>
Manually order the set fails in UpsetR
# Manually order the set fails in UpsetR

(Asked by: username_0 on 2022-12-07)

This question was also asked on Biostars
I have tried to use UpsetR to visualise the input file, which can be found here. How is it possible to make UpsetR accept an order defined in sets=c("Nlab", "NQLD", "Ngla", "Nsyl", "Ntom", "Ntab", "Natt", "Cann", "Stub", "Slyc", "Vvin", "Atha")?
with keep.order = T

with keep.order = F

> library("UpSetR")
> orthogroups_df<- read.table("orthogroups.GeneCount.tsv",  header=T, stringsAsFactors = F)
> #All species
> selected_species <- colnames(orthogroups_df)[2:(ncol(orthogroups_df) -1)] 
> selected_species
 [1] "Atha" "Cann" "NQLD" "Natt" "Ngla" "Nlab" "Nsyl" "Ntab" "Ntom" "Slyc" "Stub" "Vvin"
> head(orthogroups_df)
  Orthogroup Atha Cann NQLD Natt Ngla Nlab Nsyl Ntab Ntom Slyc Stub Vvin Total
1  OG0000000    0    0  965    0    0    3    0    0    0    0    0    0   968
2  OG0000001    0    1    3    0    0  448    0    0    0    0    0    0   452
3  OG0000002    0    1  313    0    0  120    1    0    1    0    0    0   436
4  OG0000003    0   93   15   21   46   16   33   63   36   25   39   26   413
5  OG0000004    1   42    2   34  109    6    8  154   11    9    4    0   380
6  OG0000005    0    2   61    1   34   44   91   70   43   20    1    0   367
> ncol(orthogroups_df)
[1] 14
> orthogroups_df[orthogroups_df > 0] <- 1
> # we only show intersections of interest ,  
> intersections=list(list(selected_species),
+                    list("NQLD", "Ngla", "Natt", "Nlab", "Nsyl", "Ntab", "Ntom"),
+                    list("Stub", "Slyc"),
+                    list("Atha", "Vvin"),
+                    list("Ntab", "Nsyl", "Ntom"),
+                    list("Nlab", "NQLD", "Ngla"), 
+                    list("Nlab", "NQLD", "Nsyl"), 
+                    list("Nlab", "Ngla", "Nsyl"), 
+                    list("NQLD", "Nsyl", "Ngla"))
> upset(orthogroups_df, 
+       text.scale = c(1.4),
+       sets=c("Nlab", "NQLD", "Ngla", "Nsyl", "Ntom", "Ntab", "Natt", "Cann", "Stub", "Slyc", "Vvin", "Atha"), 
+       order.by = "freq",
+       keep.order=T, 
+       intersections = intersections, 
+       sets.x.label="Total number of orthogroups", 
+       mainbar.y.label = "Number of orthogroups")

**Tags:** r, ggplot2, upsetr

---

## 1 answers

### Answer by username_1 on 2022-12-08. Score: 2 | Accepted answer

It's possible that upset is using factor levels for ordering, which is the default approach for ggplot2. If this is the case, then it can be fixed by converting the variable to a factor, and using an explicit level definition:
sets <- factor(sets, levels=sets)

For your specific example:
upSets <- c("Nlab", "NQLD", "Ngla", "Nsyl", "Ntom", "Ntab", "Natt", "Cann", "Stub", "Slyc", "Vvin", "Atha")
upSets <- factor(upSets, levels=upSets)
upset(orthogroups_df, 
   text.scale = c(1.4),
   sets=upSets, 
   order.by = "freq",
   keep.order=T, 
   intersections = intersections, 
   sets.x.label="Total number of orthogroups", 
   mainbar.y.label = "Number of orthogroups")


---

