Function Repository Resource:

UpSetChart

Source Notebook

Visualize the intersections and aggregates of intersections of tuples of intersecting sets

Contributed by: Edmund B Robinson

ResourceFunction["UpSetChart"][{tuple1,tuple2,}]

charts the list of related tuples {tuple1,tuple2,}.

ResourceFunction["UpSetChart"][{{tuple1,1,tuple1,2,},{tuple2,1,tuple2,2,},}]

charts the list of sublists of related tuples.

Details and Options

ResourceFunction["UpSetChart"] visualizes tuples of intersecting sets.
ResourceFunction["UpSetChart"] has three sections:
Set Menu ChartLower LeftBarChart of the cardinality of each element in the lists of tuples
Cardinality ChartUpper RightBarChart of the cardinality of each subset of the lists of tuples
Combination Matrix PlotLower RightPlot of the subsets in the list of tuples
ResourceFunction["UpSetChart"] takes the following options:
AspectRatio{1/GoldenRatio,1/GoldenRatio}aspect ratios used for the set menu and cardinarity sub-charts
BackgroundLighter[LightGray]style used for placeholders in combination matrix subplot
ChartLegendsNonelist of labels used for the legend for the set menu sub-chart
ChartStyleAutomaticlist of styles used for the set menu and cardinality sub-charts
ImagePadding{{20,2},{15,2}}image padding of the combination matrix subplot
ImageSize{Medium,Small}size of the combination matrix subplot
PlotMarkersGraphics[Disk[{0,0},Scaled[0.025]]]markers used for the combination matrix subplot
PlotStyleGraystyle used for the combination matrix subplot
PlotThemeAutomatictheme applied to all subplots and sub-charts
"SortingFunctions"{Identity,Identity}sort functions applied to the elements of the set menu and cardinality sub-charts
For the AspectRatio option {spec1,spec2}, spec1 is applied to the set menu sub-chart and spec2 to the cardinality sub-chart.
For the ImagePadding option {specx,specy}, specx is applied to the combination matrix subplot and cardinality sub-chart, while specy is applied to the combination matrix subplot and set menu sub-chart.
The functions in the "SortingFunctions" option {f1,f2} take an association of counts:
f1set menu sub-chartKeys are elements of the sets and Values are lists of counts in each set of tuples
f2cardinality sub-chartKeys are the subsets of the tuples and Values are lists of counts in each set of tuples

Examples

Basic Examples (2) 

Simulate data for a survey of 30 animal lovers for the types of pets they have:

In[1]:=
pets = Permutations[{"Cat", "Dog", "Fish", "Bird", "Rodent", "Reptile"}, {1, 4}];
SeedRandom[321];
pets =
 RandomChoice[
  Delete[pets, List /@ RandomSample[Range@Length@pets, Floor[.25 Length@pets]]]
  , 30
  ]
Out[3]=

View relationships:

In[4]:=
ResourceFunction["UpSetChart"][pets]
Out[4]=

Simulate data for a survey of 15 homes in two cities for the types of vehicles they own:

In[5]:=
vehicles = Permutations[{"Bicycle", "Motorbike", "Car", "Van", "Truck"}, {1, 4}];
SeedRandom[789];
vehicles =
 RandomChoice[
  Delete[vehicles, List /@ RandomSample[Range@Length@vehicles, Floor[.25 Length@vehicles]]]
  , {2, 15}
  ]
Out[6]=

View relationships of both categories:

In[7]:=
ResourceFunction["UpSetChart"][vehicles
 , ChartLegends -> {"City 1", "City 2"}
 , ChartStyle -> {Blue, Brown}
 ]
Out[7]=

Options (21) 

AspectRatio (2) 

Set menu and cardinality sub-charts are sized to the combination plot. However, their size can be adjusted with AspectRatio. Use default sizes for a fixed-sized combination plot:

In[8]:=
SeedRandom[567];
tuples = RandomSample[Range[6], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 6}], 100];
ResourceFunction["UpSetChart"][
 tuples
 , ImageSize -> {500, 150}
 ]
Out[8]=

Supply AspectRatio specification for set menu and cardinality sub-charts to adjust their size:

In[9]:=
ResourceFunction["UpSetChart"][
 tuples
 , ImageSize -> {500, 150}
 , AspectRatio -> {5/6, 1/5}
 ]
Out[9]=

Background (1) 

Supply a style for the placeholders of the combination matrix subplot:

In[10]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 20];
ResourceFunction["UpSetChart"][
 tuples
 , Background -> Yellow
 ]
Out[10]=

ChartLegends (2) 

Create example data:

In[11]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 20];

Supply a list of labels and a list of styles:

In[12]:=
ResourceFunction["UpSetChart"][
 tuples
 , ChartStyle -> {LightBlue}
 , ChartLegends -> {"Group 1"}
 ]
Out[12]=

ChartStyle (2) 

Create example data:

In[13]:=
SeedRandom[567];
tuples = Partition[
   RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 40], 20];

Supply a list of styles:

In[14]:=
ResourceFunction["UpSetChart"][
 tuples
 , ChartStyle -> {Red, Purple}
 ]
Out[14]=

ImagePadding (2) 

Set menu and cardinality sub-charts image padding uses the combination plot's image padding. Default image padding may be too narrow to fit set menu or cardinality sub-chart axis labels:

In[15]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 5000];
ResourceFunction["UpSetChart"][tuples]
Out[15]=

Supply ImagePadding specification to display full chart labels:

In[16]:=
ResourceFunction["UpSetChart"][
 tuples
 , ImagePadding -> {{25, 2}, {15, 2}}
 ]
Out[16]=

ImageSize (3) 

Default sizing compresses large combination matrix subplots:

In[17]:=
SeedRandom[567];
tuples = RandomSample[Range[6], #] & /@ RandomVariate[DiscreteUniformDistribution[{2, 5}], 100];

View with default ImageSize:

In[18]:=
ResourceFunction["UpSetChart"][
 tuples
 ]
Out[18]=

Increase ImageSize for a larger combination matrix subplot:

In[19]:=
ResourceFunction["UpSetChart"][
 tuples
 , ImageSize -> {Large, Small}
 ]
Out[19]=

PlotMarkers (2) 

Create example data:

In[20]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 20];

Set the shape of combination matrix plot markers:

In[21]:=
ResourceFunction["UpSetChart"][
  tuples
  , PlotMarkers -> Graphics@Disk[{0, 0}, Scaled[{.025, .01}]]
  ] // Rasterize
Out[21]=

PlotStyle (2) 

Set PlotStyle of combination matrix plot markers:

In[22]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 20];

View with PlotStyle specification:

In[23]:=
ResourceFunction["UpSetChart"][
 tuples
 , PlotStyle -> {Brown}
 ]
Out[23]=

PlotTheme (2) 

Create example data:

In[24]:=
SeedRandom[567];
tuples = Partition[
   RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 40], 20];

View with "Detailed" PlotTheme:

In[25]:=
ResourceFunction["UpSetChart"][
 tuples
 , PlotTheme -> "Detailed"
 ]
Out[25]=

SortingFunctions (3) 

Create example data:

In[26]:=
SeedRandom[567];
tuples = Partition[
   RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 40], 20];

Unsorted elements and subsets are listed as they appear in the data:

In[27]:=
ResourceFunction["UpSetChart"][
 tuples
 ]
Out[27]=

Sort the set menu sub-chart by its labels and the cardinality sub-chart by the total across all sets in reverse order:

In[28]:=
ResourceFunction["UpSetChart"][
 tuples
 , "SortingFunctions" -> {KeySort, ReverseSortBy[Total]}
 ]
Out[28]=

Possible Issues (3) 

Both the ChartStyle and ChartLegends options must be supplied to return a legend:

In[29]:=
SeedRandom[567];
tuples = RandomSample[Range[4], #] & /@ RandomVariate[DiscreteUniformDistribution[{1, 4}], 20];

Without both options:

In[30]:=
ResourceFunction["UpSetChart"][
 tuples
 , ChartLegends -> {"Group 1"}
 ]
Out[30]=

With both options:

In[31]:=
ResourceFunction["UpSetChart"][
 tuples
 , ChartStyle -> {Gray}
 , ChartLegends -> {"Group 1"}
 ]
Out[31]=

Neat Examples (1) 

Explore the intersecting sets of class, sex and survived in the Titanic dataset:

In[32]:=
DynamicModule[
 {
  data = Query[All, {"class", "sex", "survived"}]@
    ExampleData[{"Dataset", "Titanic"}, "RawData"]
  , schema = ExampleData[{"Dataset", "Titanic"}, "Schema"]
  , groupedData
  , groupList = {"class", "sex", "survived"}
  , sorting = {KeySort -> "Labels", SortBy[Total] -> "Total", ReverseSortBy[Total] -> "Reverse Total"}
  , chartStyles =
   AssociationThread[
    {"1st", "2nd", "3rd", "male", "female", True, False} ->
     ColorData["Crayola"] /@ {"Gold", "Silver", "Copper", "PurpleHeart", "MangoTango", "Sunglow", "OuterSpace"}
    ]
  }
 , Manipulate[
  groupedData = Query[GroupBy[#[groupBy] &], All, Complement[groupList, {groupBy}]/*Values]@data;
  ResourceFunction["UpSetChart"][
   Values[groupedData]
   , ChartStyle -> Lookup[chartStyles, Keys[groupedData]]
   , ChartLegends -> Keys[groupedData]
   , "SortingFunctions" -> {elementSort, setSort}
   ]
  , {{groupBy, First@groupList, "Group By"}, groupList}
  , {{elementSort, KeySort, "Sort Elements"}, Insert[sorting, KeySortBy[Position[schema, #] &] -> "Schema", 2], ControlType -> PopupMenu}
  , {{setSort, KeySort, "Sort Sets"}, sorting, ControlType -> PopupMenu}
  , TrackedSymbols :> True
  ]
 , SaveDefinitions -> True
 , TrackedSymbols :> {}
 ]
Out[32]=

Publisher

Edmund B Robinson

Version History

  • 1.0.0 – 16 August 2021

Source Metadata

Related Resources

License Information