### Install ComplexUpset from GitHub Source: https://krassowski.github.io/complex-upset/index.html Installs the most recent version of the ComplexUpset package directly from GitHub using the devtools package. Ensure devtools is installed first. ```R if(!require(devtools)) install.packages("devtools") devtools::install_github("krassowski/complex-upset") ``` -------------------------------- ### Install ComplexUpset using Conda/Mamba Source: https://krassowski.github.io/complex-upset/index.html Installs the ComplexUpset package for R using the conda/mamba package managers from the conda-forge channel. ```shell conda install -c conda-forge r-complexupset ``` -------------------------------- ### create_upset_abc_example Source: https://krassowski.github.io/complex-upset/reference/create_upset_abc_example.html Generates an example dataset with three sets labeled A, B, and C, suitable for use with Upset plots. ```APIDOC ## create_upset_abc_example() ### Description Creates an example dataset with three sets: A, B and C. ### Method ``` create_upset_abc_example() ``` ### Parameters This function does not take any parameters. ### Request Example ```R create_upset_abc_example() ``` ### Response Returns a data frame representing the example dataset with three sets (A, B, C). ``` -------------------------------- ### Install ComplexUpset from CRAN Source: https://krassowski.github.io/complex-upset/index.html Installs a stable CRAN release of the ComplexUpset package. This version may be slightly behind the latest GitHub release. ```R install.packages('ComplexUpset') ``` -------------------------------- ### Install Python Requirements Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Install necessary Python packages including pandas, pydataset, and rpy2. These are required for data manipulation and R integration. ```bash pip install pandas pydataset rpy2 ``` -------------------------------- ### Create Example Dataset with Sets A, B, C Source: https://krassowski.github.io/complex-upset/reference/create_upset_abc_example.html Use this function to generate a sample dataset for Upset plots. It creates three sets: A, B, and C. ```R create_upset_abc_example() ``` -------------------------------- ### Adding Custom Themes for Annotations in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example shows how to add custom themes for specific annotation panels like 'Length' and 'Rating' in an upset plot. ```r set_size(8, 8) upset( movies, genres, annotations = list( 'Length'=list( aes=aes(x=intersection, y=length), geom=geom_boxplot(na.rm=TRUE) ), 'Rating'=list( aes=aes(x=intersection, y=rating), geom=list( geom_jitter(aes(color=log10(votes)), na.rm=TRUE), geom_violin(alpha=0.5, na.rm=TRUE) ) ) ), min_size=10, width_ratio=0.1, themes=modifyList( upset_themes, list(Rating=theme_void(), Length=theme()) ) ) ``` -------------------------------- ### upset_set_size Function Example Source: https://krassowski.github.io/complex-upset/reference/upset_set_size.html Use this function to configure how set sizes are displayed in an Upset plot. It accepts arguments for aesthetics, geometry, position, and filtering. ```R upset_set_size( mapping = aes(), geom = geom_bar(width = 0.6), position = "left", filter_intersections = FALSE ) ``` -------------------------------- ### Modify All Default Themes at Once Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Adjust all default themes simultaneously using `upset_default_themes()`. This example changes the text color for all default theme elements to red. ```R upset( movies, genres, min_size=10, width_ratio=0.1, themes=upset_default_themes(text=element_text(color='red')) ) ``` -------------------------------- ### Highlighting Intersections and Sets with Queries in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example demonstrates using `upset_query()` to highlight specific intersections and sets by modifying their aesthetics (color, fill) and specifying components to apply the query to. ```r set_size(8, 6) upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, annotations = list( 'Length'=list( aes=aes(x=intersection, y=length), geom=geom_boxplot(na.rm=TRUE) ) ), queries=list( upset_query( intersect=c('Drama', 'Comedy'), color='red', fill='red', only_components=c('intersections_matrix', 'Intersection size') ), upset_query( set='Drama', fill='blue' ), upset_query( intersect=c('Romance', 'Comedy'), fill='yellow', only_components=c('Length') ) ) ) ``` -------------------------------- ### Highlight specific regions in a Venn diagram Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example demonstrates how to highlight specific regions in a Venn diagram. It allows for custom highlighting of selected sets and sets inactive regions to white. ```r set_size(8, 5.5) ( ggplot(arranged) + theme_void() + coord_fixed() + geom_venn_region(movies_subset, sets=genres_subset, alpha=0.2) + geom_point(aes(x=x, y=y, color=region), size=1.5) + geom_venn_circle(movies_subset, sets=genres_subset, size=2) + geom_venn_label_set(movies_subset, sets=genres_subset, aes(label=region), outwards_adjust=2.6) + scale_color_venn_mix(movies, sets=genres_subset, guide='none') + scale_fill_venn_mix(movies, sets=genres_subset, guide='none', highlight=c('Comedy-Action', 'Drama'), inactive_color='white') ) ``` -------------------------------- ### Define and Use Custom Statistical Tests Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example demonstrates how to define custom test functions for specific variables and use them with upset_test. It includes wrapper functions for 'chisq.test' and 'aov' to match the expected output signature. The 'tests' argument is used to apply these custom tests. ```R chisq_from_formula = function(formula, data) { chisq.test( ftable(formula, data) ) } anova_single = function(formula, data) { result = summary(aov(formula, data)) list( p.value=result[[1]][['Pr(>F)']][[1]], method='Analysis of variance Pr(>F)', statistic=result[[1]][['F value']][[1]] ) } custom_tests = list( mpaa=chisq_from_formula, budget=anova_single ) ``` ```R head(upset_test(movies, genres, tests=custom_tests)) ``` -------------------------------- ### Get Upset Plot Theme Names Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Retrieve the names of available default themes for upset plots using `names(upset_themes)`. This helps in understanding which components can be themed. ```R names(upset_themes) ``` -------------------------------- ### Modifying All Default Themes in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `upset_default_themes()` to modify all default themes at once, for example, changing text color to red. ```r set_size(8, 4) upset( movies, genres, min_size=10, width_ratio=0.1, themes=upset_default_themes(text=element_text(color='red')) ) ``` -------------------------------- ### R: Combine Upset Plots with Patchwork Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Combines two upset plots side-by-side using the `patchwork` library. Ensures consistent annotations and guides are collected. ```R %%R -w 800 -h 500 library(patchwork) annotations = list( 'MPAA Rating'=list( aes=aes(x=intersection, fill=mpaa), geom=list( geom_bar(stat='count', position='fill') ) ) ) set.seed(0) # for replicable example only data_1 = movies[sample(nrow(movies), 100), ] data_2 = movies[sample(nrow(movies), 100), ] u1 = upset(data_1, genres, min_size=5, base_annotations=annotations) u2 = upset(data_2, genres, min_size=5, base_annotations=annotations) (u1 | u2) + plot_layout(guides='collect') ``` -------------------------------- ### Highlight Intersections and Sets with Queries Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Use `upset_query()` to highlight specific intersections or sets in an upset plot. This example demonstrates highlighting a specific intersection, a set, and another intersection with different colors and components. ```R upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, annotations = list( 'Length'=list( aes=aes(x=intersection, y=length), geom=geom_boxplot(na.rm=TRUE) ) ), queries=list( upset_query( intersect=c('Drama', 'Comedy'), color='red', fill='red', only_components=c('intersections_matrix', 'Intersection size') ), upset_query( set='Drama', fill='blue' ), upset_query( intersect=c('Romance', 'Comedy'), fill='yellow', only_components=c('Length') ) ) ) ``` -------------------------------- ### Define Custom Statistical Tests for upset_test Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html This snippet shows how to define custom test functions for specific variables when using upset_test. It includes examples for Chi-squared test and ANOVA, demonstrating how to wrap them to match the expected output format. ```R chisq_from_formula = function(formula, data) { chisq.test( ftable(formula, data) ) } anova_single = function(formula, data) { result = summary(aov(formula, data)) list( p.value=result[[1]][['Pr(>F)']][[1]], method='Analysis of variance Pr(>F)', statistic=result[[1]][['F value']][[1]] ) } custom_tests = list( mpaa=chisq_from_formula, budget=anova_single ) ``` -------------------------------- ### Displaying Counts with geom_text() Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Add counts to the bars using `geom_text()` and `stat='count'`. Includes an example of adding a specific annotation with `annotate()` and expanding y-axis limits. ```R set_size(5, 3) upset( movies, genres, min_size=10, width_ratio=0.3, encode_sets=FALSE, # for annotate() to select the set by name disable encoding set_sizes=( upset_set_size() + geom_text(aes(label=..count..), hjust=1.1, stat='count') # you can also add annotations on top of bars: + annotate(geom='text', label='@', x='Drama', y=850, color='white', size=3) + expand_limits(y=1100) + theme(axis.text.x=element_text(angle=90)) ) ) ``` -------------------------------- ### Substitute Default Theme in Upset Plot Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Replace the default theme of an upset plot with a custom theme using the `themes` argument. This example substitutes all default themes with a basic `theme()`. ```R upset(movies, genres, min_size=10, themes=list(default=theme())) ``` -------------------------------- ### Modify Specific Default Themes Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Customize specific default themes using `upset_modify_themes()`. This example increases the text size for the 'intersections_matrix' theme and sets the x-axis text angle for 'overall_sizes'. ```R upset( movies, genres, base_annotations=list('Intersection size'=intersection_size(counts=FALSE)), min_size=100, width_ratio=0.1, themes=upset_modify_themes( list( 'intersections_matrix'=theme(text=element_text(size=20)), 'overall_sizes'=theme(axis.text.x=element_text(angle=90)) ) ) ) ``` -------------------------------- ### Add Custom Themes for Annotations in Upset Plot Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Incorporate custom themes for specific annotation panels in an upset plot. This example applies `theme_void()` to the 'Rating' annotation and `theme()` to the 'Length' annotation, merging them with existing themes. ```R upset( movies, genres, annotations = list( 'Length'=list( aes=aes(x=intersection, y=length), geom=geom_boxplot(na.rm=TRUE) ), 'Rating'=list( aes=aes(x=intersection, y=rating), geom=list( geom_jitter(aes(color=log10(votes)), na.rm=TRUE), geom_violin(alpha=0.5, na.rm=TRUE) ) ) ), min_size=10, width_ratio=0.1, themes=modifyList( upset_themes, list(Rating=theme_void(), Length=theme()) ) ) ``` -------------------------------- ### Display proportions of categorical variables using bar plots Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use bar plots within annotations to visualize the proportions of categorical variables across intersections. This example shows how to display MPAA rating proportions, using `position='fill'` for stacked bars and `scales::percent_format()` for y-axis labels. ```R set_size(8, 5) upset( movies, genres, annotations = list( 'MPAA Rating'=( ggplot(mapping=aes(fill=mpaa)) + geom_bar(stat='count', position='fill') + scale_y_continuous(labels=scales::percent_format()) + scale_fill_manual(values=c( 'R'='#E41A1C', 'PG'='#377EB8', 'PG-13'='#4DAF4A', 'NC-17'='#FF7F00' )) + ylab('MPAA Rating') ) ), width_ratio=0.1 ) ``` -------------------------------- ### Prepare Movies Dataset in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Loads the ggplot2 movies dataset and displays the first few rows. This is a preliminary step for data analysis. ```r movies = as.data.frame(ggplot2movies::movies) head(movies, 3) ``` -------------------------------- ### Define Color Scales for Plotting Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Defines custom color scales for MPAA ratings and for showing/hiding elements, used in subsequent percentage display examples. ```R %%R rating_scale = scale_fill_manual(values=c( 'R'='#E41A1C', 'PG'='#377EB8', 'PG-13'='#4DAF4A', 'NC-17'='#FF7F00' )) show_hide_scale = scale_color_manual(values=c('show'='black', 'hide'='transparent'), guide='none') ``` -------------------------------- ### Load and Prepare Movie Dataset Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Load the 'movies' dataset using pydataset and drop rows with missing values. Display the transpose of the first three rows. ```python from pydataset import data as load_data movies = load_data('movies').dropna() movies.head(3).T ``` -------------------------------- ### R: Prepare Data for Venn Diagrams Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Prepares a subset of movie data for Venn diagram visualization. Arranges data points based on specified genres and calculates a 'good_rating' flag. ```R %%R movies_subset = head(movies, 300) genres_subset = c('Comedy', 'Drama', 'Action') movies_subset$good_rating = movies_subset$rating > mean(movies_subset$rating) arranged = arrange_venn(movies_subset, sets=genres_subset) ``` -------------------------------- ### Change Position and Add Fill to Set Sizes (R) Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Customize set size bars using `geom_bar` with `aes(fill=...)` and control their position with `position='right'`. Use `guides='over'` to move legends. ```R upset( movies, genres, min_size=10, width_ratio=0.3, set_sizes=( upset_set_size( geom=geom_bar( aes(fill=mpaa, x=group), width=0.8 ), position='right' ) ), # moves legends over the set sizes guides='over' ) ``` -------------------------------- ### Sort Intersections by Ratio in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `sort_intersections_by='ratio'` to sort intersections by their relative frequency within the dataset. This example also includes annotations for intersection size and ratio. ```r set_size(8, 4) upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, sort_intersections_by='ratio', base_annotations=list( 'Intersection size'=intersection_size(text_mapping=aes(label=!!upset_text_percentage())), 'Intersection ratio'=intersection_ratio(text_mapping=aes(label=!!upset_text_percentage())) ) ) ``` -------------------------------- ### Substituting Default Themes in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Demonstrates substituting the default theme for an upset plot using the `themes` argument with `theme()`. ```r set_size(8, 4) upset(movies, genres, min_size=10, themes=list(default=theme())) ``` -------------------------------- ### Show Intersection Size and Ratio Annotations Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Display both the raw intersection size and the intersection/union size ratio annotations. The ratio helps identify intersections that are larger than expected by chance, assuming equal probability of set membership. ```R set_size(8, 6) upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, base_annotations=list( 'Intersection size'=intersection_size(), 'Intersection ratio'=intersection_ratio() ) ) ``` -------------------------------- ### Changing Position and Adding Fill Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Customize the bar geometry with `geom_bar` to include fill aesthetics based on data and adjust the position. The `guides='over'` argument moves legends. ```R set_size(5, 3) upset( movies, genres, min_size=10, width_ratio=0.3, set_sizes=( upset_set_size( geom=geom_bar( aes(fill=mpaa, x=group), width=0.8 ), position='right' ) ), # moves legends over the set sizes guides='over' ) ``` -------------------------------- ### Adjusting Intersection Matrix in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `intersection_matrix()` to modify matrix parameters like geom, segment, and outline colors. This example also demonstrates custom color scales and annotations. ```r set_size(8, 4) upset( movies, genres, name='genre', min_size=10, encode_sets=FALSE, # for annotate() to select the set by name disable encoding matrix=( intersection_matrix( geom=geom_point( shape='square', size=3.5 ), segment=geom_segment( linetype='dotted' ), outline_color=list( active='darkorange3', inactive='grey70' ) ) + scale_color_manual( values=c('TRUE'='orange', 'FALSE'='grey'), labels=c('TRUE'='yes', 'FALSE'='no'), breaks=c('TRUE', 'FALSE'), name='Is intersection member?' ) + scale_y_discrete( position='right' ) + annotate( geom='text', label='Look here →', x='Comedy-Drama', y='Drama', size=5, hjust=1 ) ), queries=list( upset_query( intersect=c('Drama', 'Comedy'), color='red', fill='red', only_components=c('intersections_matrix', 'Intersection size') ) ) ) ``` -------------------------------- ### Create a two-set Venn diagram Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This snippet shows how to create a two-set Venn diagram. The point density is adjusted to fit all points from the set with the largest space restrictions. ```r set_size(6, 4.5) genres_subset = c('Action', 'Drama') ( ggplot(arrange_venn(movies_subset, sets=genres_subset)) + theme_void() + coord_fixed() + geom_point(aes(x=x, y=y, color=region), size=2) + geom_venn_circle(movies_subset, sets=genres_subset, size=2) + geom_venn_label_set(movies_subset, sets=genres_subset, aes(label=region), outwards_adjust=2.6) + scale_color_venn_mix(movies, sets=genres_subset, guide='none') ) ``` -------------------------------- ### Adjusting Annotation Height in Upset Plots Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example shows how to control the relative height of annotations within an upset plot. It uses 'patchwork::plot_layout' to specify the heights for the main plot and its annotations. ```R set_size(8, 3.5) upset( movies, genres, name='genre', width_ratio=0.1, min_size=100, annotations =list( 'MPAA Rating'=list( aes=aes(x=intersection, fill=mpaa), geom=list( geom_bar(stat='count', position='fill'), scale_y_continuous(labels=scales::percent_format()) ) ) ) ) + patchwork::plot_layout(heights=c(0.5, 1, 0.5)) ``` -------------------------------- ### Venn Diagram with Highlighted Regions Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Creates a Venn diagram with highlighted regions to visualize different intersection and union modes. Uses `scale_fill_venn_mix` with the `highlight` argument. ```R simple_venn = ( abc_venn + geom_venn_region(data=abc_data, alpha=0.3) + geom_point(aes(x=x, y=y), size=0.75, alpha=0.3) + geom_venn_circle(abc_data) + geom_venn_label_set(abc_data, aes(label=region), outwards_adjust=2.55) ) highlight = function(regions) scale_fill_venn_mix( abc_data, guide='none', highlight=regions, inactive_color='NA' ) ( ( simple_venn + highlight(c('A-B')) + labs(title='Exclusive intersection of A and B') | simple_venn + highlight(c('A-B', 'A-B-C')) + labs(title='Inclusive intersection of A and B') ) / ( simple_venn + highlight(c('A-B', 'A', 'B')) + labs(title='Exclusive union of A and B') | simple_venn + highlight(c('A-B', 'A-B-C', 'A', 'B', 'A-C', 'B-C')) + labs(title='Inclusive union of A and B') ) ) ``` -------------------------------- ### Create a reverse logarithmic scale Source: https://krassowski.github.io/complex-upset/reference/reverse_log_trans.html Use reverse_log_trans to create a logarithmic scale. Specify the logarithm base as an argument. ```R reverse_log_trans(base = 10) ``` -------------------------------- ### Make Plot Transparent (R) Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Set the plot background to transparent using `theme(plot.background=element_rect(fill='transparent', color=NA))`. Adjust stripe colors for visibility. Use `ggsave(..., bg="transparent")` for exporting. ```R ( upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, stripes=c(alpha('grey90', 0.45), alpha('white', 0.3)) ) & theme(plot.background=element_rect(fill='transparent', color=NA)) ) ``` -------------------------------- ### Display Intersection Percentages within Intersection in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `aes_percentage(relative_to='intersection')` within `geom_text` to display the percentage of each intersection relative to the total size of that intersection. This example highlights 'R' rated movies. ```r rating_scale = scale_fill_manual(values=c( 'R'='#E41A1C', 'PG'='#377EB8', 'PG-13'='#4DAF4A', 'NC-17'='#FF7F00' )) show_hide_scale = scale_color_manual(values=c('show'='black', 'hide'='transparent'), guide='none') set_size(8, 5) upset( movies, genres, name='genre', width_ratio=0.1, min_size=100, annotations =list( 'MPAA Rating'=list( aes=aes(x=intersection, fill=mpaa), geom=list( geom_bar(stat='count', position='fill', na.rm=TRUE), geom_text( aes( label=!!aes_percentage(relative_to='intersection'), color=ifelse(mpaa == 'R', 'show', 'hide') ), stat='count', position=position_fill(vjust = .5) ), scale_y_continuous(labels=scales::percent_format()), show_hide_scale, rating_scale ) ) ) ) ``` -------------------------------- ### Display Intersection Percentages Relative to Group in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `aes_percentage(relative_to='group')` within `geom_text` to display the percentage of each intersection relative to its parent group (e.g., MPAA rating). This example highlights 'R' rated movies. ```r set_size(8, 5) upset( movies, genres, name='genre', width_ratio=0.1, min_size=100, annotations =list( 'MPAA Rating'=list( aes=aes(x=intersection, fill=mpaa), geom=list( geom_bar(stat='count', position='fill', na.rm=TRUE), geom_text( aes( label=!!aes_percentage(relative_to='group'), group=mpaa, color=ifelse(mpaa == 'R', 'show', 'hide') ), stat='count', position=position_fill(vjust = .5) ), scale_y_continuous(labels=scales::percent_format()), show_hide_scale, rating_scale ) ) ) ) ``` -------------------------------- ### Show Intersection Size/Union Size Ratio Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Visualize the ratio of intersection size to union size using `intersection_ratio()`. This helps identify intersections that are larger than expected by chance, indicating significant overlap. ```R upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, base_annotations=list( 'Intersection size'=intersection_size(), 'Intersection ratio'=intersection_ratio() ) ) ``` -------------------------------- ### R: Venn Diagram Highlighting All Regions Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Creates a Venn diagram that highlights all regions with semi-transparency. Displays points, circles, and labels for each set and region, using custom color and fill scales. ```R %%R -w 800 -h 550 ( ggplot(arranged) + theme_void() + coord_fixed() + geom_venn_region(movies_subset, sets=genres_subset, alpha=0.1) + geom_point(aes(x=x, y=y, color=region), size=2.5) + geom_venn_circle(movies_subset, sets=genres_subset, size=1.5) + geom_venn_label_set(movies_subset, sets=genres_subset, aes(label=region), outwards_adjust=2.6) + geom_venn_label_region(movies_subset, sets=genres_subset, aes(label=size), position=position_nudge(y=0.15)) + scale_color_venn_mix(movies, sets=genres_subset, guide='none') + scale_fill_venn_mix(movies, sets=genres_subset, guide='none') ) ``` -------------------------------- ### aes_percentage Function Signature Source: https://krassowski.github.io/complex-upset/reference/aes_percentage.html Defines the signature and arguments for the aes_percentage function. ```APIDOC ## aes_percentage(relative_to, digits = 0, sep = "") ### Description Generate mapping for labeling percentages. ### Arguments * **relative_to** (character) - defines proportion that should be calculated, relative to `'intersection'`, `'group'`, or `'all'` observed values. * **digits** (integer) - number of digits to show (default=0). * **sep** (character) - separator between the digit and percent sign (no separator by default). ``` -------------------------------- ### scale_fill_venn_mix Source: https://krassowski.github.io/complex-upset/reference/scale_fill_venn_mix.html Creates a fill scale for Venn diagrams, allowing customization of colors based on set membership. It accepts arguments passed to `scale_color_venn_mix` and has an additional `na.value` argument for elements not belonging to any set. ```APIDOC ## scale_fill_venn_mix ### Description Creates a fill scale for Venn diagrams. ### Arguments - `...`: Arguments passed on to `scale_color_venn_mix`. - `na.value` (any): Value for elements not belonging to any of the known sets. Defaults to "NA". ### Arguments passed on to `scale_color_venn_mix` - `data` (dataframe): A dataframe including binary columns representing membership in sets. - `sets` (vector): Vector with names of columns representing membership in sets. - `colors` (named list): Named list of colors for sets (one set=one color). - `highlight` (any): Which regions of the diagram to highlight. - `active_color` (any): Color for highlight. - `inactive_color` (any): Color for lack of highlight. - `scale` (function): The base scale (default=`scale_color_manual()`). ``` -------------------------------- ### Modify set size geoms and layers Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Customize the appearance of the set size plot by changing the geom (e.g., `geom_bar`, `geom_point`) or by providing a custom list of `ggplot2` layers. This example shows using `geom_bar`, `geom_point`, and scaling y-axis. ```R set_size(8, 3) ( upset( movies, genres, width_ratio=0.5, max_size=100, min_size=15, wrap=TRUE, set_sizes=upset_set_size( geom=geom_bar(width=0.4) ) ) + upset( movies, genres, width_ratio=0.5, max_size=100, min_size=15, wrap=TRUE, set_sizes=upset_set_size( geom=geom_point( stat='count', color='blue' ) ) ) + upset( movies, genres, width_ratio=0.5, max_size=100, min_size=15, wrap=TRUE, set_sizes=( upset_set_size( geom=geom_point(stat='count'), mapping=aes(y=..count../max(..count..)), ) + ylab('Size relative to the largest') ) ) ) ``` -------------------------------- ### Upset Plot with Conditional Text Labels Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This example demonstrates how to display text labels on specific bars within an upset plot's annotation. It conditionally shows 'R' for 'MPAA Rating' bars where the rating is 'R', otherwise displaying NA. ```R set_size(8, 5) upset( movies, genres, name='genre', width_ratio=0.1, min_size=100, annotations =list( 'MPAA Rating'=list( aes=aes(x=intersection, fill=mpaa), geom=list( geom_bar(stat='count', position='fill', na.rm=TRUE), geom_text( aes(label=ifelse(mpaa == 'R', 'R', NA)), stat='count', position=position_fill(vjust = .5), na.rm=TRUE ), show_hide_scale, rating_scale ) ) ) ) ``` -------------------------------- ### Highlight Set with upset_query Source: https://krassowski.github.io/complex-upset/reference/upset_query.html Use to highlight a single set by providing its name to the 'set' argument. Customization of fill color is demonstrated. ```R upset_query(set='Drama', fill='blue') ``` -------------------------------- ### Venn Diagram with Regions and Points Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Generates a Venn diagram with specified regions, points, circles, and labels. Requires data prepared with `arrange_venn` and uses `ggplot2` for plotting. ```R abc_data = create_upset_abc_example() abc_venn = ( ggplot(arrange_venn(abc_data)) + coord_fixed() + theme_void() + scale_color_venn_mix(abc_data) ) ( abc_venn + geom_venn_region(data=abc_data, alpha=0.05) + geom_point(aes(x=x, y=y, color=region), size=1) + geom_venn_circle(abc_data) + geom_venn_label_set(abc_data, aes(label=region)) + geom_venn_label_region( abc_data, aes(label=size), outwards_adjust=1.75, position=position_nudge(y=0.2) ) + scale_fill_venn_mix(abc_data, guide='none') ) ``` -------------------------------- ### Load rpy2 IPython Extension Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Load the rpy2 extension for IPython, which enables the use of R magic commands within Jupyter Notebooks. ```ipython %%capture %load_ext rpy2.ipython ``` -------------------------------- ### Modify Default UpSet Themes Source: https://krassowski.github.io/complex-upset/reference/upset_default_themes.html Use `upset_default_themes(...)` to apply modifications to the default UpSet themes. Any arguments provided are passed directly to the `theme()` function for customization. ```R upset_default_themes(...) ``` -------------------------------- ### Run upset_test with Minimum Sample Size and Bartlett Test Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html This snippet shows how to use the upset_test function with a specific test (bartlett.test) and a minimum sample size constraint (`min_size=2`). This is useful for tests that require a minimum number of observations per group and to suppress warnings for tests that cannot be performed. ```R bartlett_results = suppressWarnings(upset_test(movies, genres, test=bartlett.test, min_size=2)) tail(bartlett_results) ``` ```text [1] "NA, year, length, budget, rating, votes, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, NA differ significantly between intersections" variable p.value statistic year year 1.041955e-67 386.53699 length length 3.982729e-67 383.70148 budget budget 7.637563e-50 298.89911 rating rating 3.980194e-06 66.63277 title title NA NA mpaa mpaa NA NA test fdr year Bartlett test of homogeneity of variances 1.302444e-67 length Bartlett test of homogeneity of variances 4.595457e-67 budget Bartlett test of homogeneity of variances 8.183103e-50 rating Bartlett test of homogeneity of variances 3.980194e-06 title Bartlett test of homogeneity of variances NA ``` -------------------------------- ### Sort Intersections by Ratio with Annotations Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Sorts intersections by ratio and includes annotations for intersection size and ratio, displaying percentages. ```R %%R -w 800 -h 400 upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, sort_intersections_by='ratio', base_annotations=list( 'Intersection size'=intersection_size(text_mapping=aes(label=!!upset_text_percentage())), 'Intersection ratio'=intersection_ratio(text_mapping=aes(label=!!upset_text_percentage())) ) ) ``` -------------------------------- ### upset_default_themes Source: https://krassowski.github.io/complex-upset/reference/upset_default_themes.html Retrieves the default UpSet themes, allowing for modifications via provided arguments. These arguments are passed directly to the `theme()` function. ```APIDOC ## upset_default_themes(...) ### Description Returns the default UpSet themes with all themes modified with provided arguments. ### Arguments `...`: Arguments passed to `theme()`. ``` -------------------------------- ### Run Default Statistical Tests with upset_test Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html This snippet demonstrates the basic usage of the upset_test function with default settings to identify variables that differ significantly between intersections. It uses the Kruskal-Wallis rank sum test by default. ```R %R upset_test(movies, genres) ``` ```text [1] "year, length, budget, rating, votes, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, mpaa differ significantly between intersections" ``` -------------------------------- ### Load R Libraries Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Load the ggplot2 and ComplexUpset libraries in R. These are essential for plotting and creating upset plots. ```R library(ggplot2) library(ComplexUpset) ``` -------------------------------- ### Highlight all regions in a Venn diagram Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use this snippet to highlight all regions in a Venn diagram. It sets the plot size, defines subsets, and uses geom_venn_region, geom_point, geom_venn_circle, geom_venn_label_set, geom_venn_label_region, and scale_color_venn_mix for visualization. ```r set_size(8, 5.5) ( ggplot(arranged) + theme_void() + coord_fixed() + geom_venn_region(movies_subset, sets=genres_subset, alpha=0.1) + geom_point(aes(x=x, y=y, color=region), size=2.5) + geom_venn_circle(movies_subset, sets=genres_subset, size=1.5) + geom_venn_label_set(movies_subset, sets=genres_subset, aes(label=region), outwards_adjust=2.6) + geom_venn_label_region(movies_subset, sets=genres_subset, aes(label=size), position=position_nudge(y=0.15)) + scale_color_venn_mix(movies, sets=genres_subset, guide='none') + scale_fill_venn_mix(movies, sets=genres_subset, guide='none') ) ``` -------------------------------- ### arrange_venn Function Source: https://krassowski.github.io/complex-upset/reference/arrange_venn.html This function takes a dataframe with binary set membership columns and arranges points for a Venn diagram. It offers several parameters to control the arrangement process, including the radius of circles, maximum iterations, and options for extracting set or region data. ```APIDOC ## arrange_venn ### Description Arranges points for a Venn diagram based on set membership. ### Signature ```R arrange_venn( data, sets = NULL, radius = 1.5, max_iterations = 10, verbose = FALSE, outwards_adjust = 1.3, extract_sets = FALSE, extract_regions = FALSE, repeat_in_intersections = FALSE, starting_grid_size = "auto" ) ``` ### Arguments * **data** (dataframe) - A dataframe including binary columns representing membership in sets. * **sets** (vector, optional) - Vector with names of columns representing membership in sets. Defaults to NULL. * **radius** (numeric) - The radius of the circle. Defaults to 1.5. * **max_iterations** (numeric) - The maximal number of iterations. Defaults to 10. * **verbose** (logical) - Should debugging notes be printed? Defaults to FALSE. * **outwards_adjust** (numeric) - The multiplier defining the distance from the centre. Defaults to 1.3. * **extract_sets** (logical) - Should only sets be extracted? Defaults to FALSE. * **extract_regions** (logical) - Should all unique regions be extracted? Defaults to FALSE. * **repeat_in_intersections** (logical) - Repeat intersection k times where k is the number of sets it belongs to? Defaults to FALSE. * **starting_grid_size** (character or numeric) - The starting size of the grid for placement of elements. Defaults to "auto". ``` -------------------------------- ### Display ticks on the set size x-axis Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Show x-axis ticks for the set size plot by adding `theme(axis.ticks.x=element_line())`. This can be combined with other theme adjustments. ```R set_size(4, 3) upset(movies, genres, width_ratio=0.3, min_size=100, set_sizes=( upset_set_size() + theme(axis.ticks.x=element_line()) ) ) ``` -------------------------------- ### Show intersection percentages with text_mapping Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Use `intersection_size` with `text_mapping` to display the percentage of items in the intersection relative to the union size. The `upset_text_percentage` shorthand simplifies this. ```R set_size(8, 6) upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, base_annotations=list( # with manual aes specification: 'Intersection size'=intersection_size(text_mapping=aes(label=paste0(round( !!get_size_mode('exclusive_intersection')/!!get_size_mode('inclusive_union') * 100 ), '%'))), # using shorthand: 'Intersection ratio'=intersection_ratio(text_mapping=aes(label=!!upset_text_percentage())) ) ) ``` -------------------------------- ### Basic Upset Plot with Genre Data in R Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Generates a basic upset plot using the ComplexUpset package. It visualizes the intersections of movie genres, setting plot dimensions and adjusting the width ratio for the set size panel. ```r set_size(8, 3) upset(movies, genres, name='genre', width_ratio=0.1) ``` -------------------------------- ### Add Multiple Annotation Components to Upset Plot Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Demonstrates three methods for adding annotation components (panels) to an Upset plot: passing a list of ggplot objects, using ggplot directly, and using the upset_annotate shorthand. Ensure necessary libraries are loaded before use. ```R %%R -w 800 -h 800 set.seed(0) # keep the same jitter for identical plots upset( movies, genres, annotations = list( # 1st method - passing list: 'Length'=list( aes=aes(x=intersection, y=length), # provide a list if you wish to add several geoms geom=geom_boxplot(na.rm=TRUE) ), # 2nd method - using ggplot 'Rating'=( # note that aes(x=intersection) is supplied by default and can be skipped ggplot(mapping=aes(y=rating)) # checkout ggbeeswarm::geom_quasirandom for better results! + geom_jitter(aes(color=log10(votes)), na.rm=TRUE) + geom_violin(alpha=0.5, na.rm=TRUE) ), # 3rd method - using `upset_annotate` shorthand 'Budget'=upset_annotate('budget', geom_boxplot(na.rm=TRUE)) ), min_size=10, width_ratio=0.1 ) ``` -------------------------------- ### Display Intersection Percentages Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html Show percentages of items in intersections relative to the potential size of the intersection. Use `upset_text_percentage()` for a convenient shorthand to format these percentages. ```R upset( movies, genres, name='genre', width_ratio=0.1, min_size=10, base_annotations=list( # with manual aes specification: 'Intersection size'=intersection_size(text_mapping=aes(label=paste0(round( !!get_size_mode('exclusive_intersection')/!!get_size_mode('inclusive_union') * 100 ), '%'))), # using shorthand: 'Intersection ratio'=intersection_ratio(text_mapping=aes(label=!!upset_text_percentage())) ) ) ``` -------------------------------- ### Run Default Statistical Tests with upset_test Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html This snippet shows the basic usage of the upset_test function to perform default statistical tests (Kruskal-Wallis rank sum test) on the 'movies' dataset, comparing 'genres'. The output indicates which variables differ significantly between intersections. ```R upset_test(movies, genres) ``` -------------------------------- ### Run upset_test with Custom Tests Source: https://krassowski.github.io/complex-upset/articles/Examples_Python.html This snippet demonstrates how to use the custom_tests defined previously with the upset_test function. It applies the custom Chi-squared test for 'mpaa' and ANOVA for 'budget', while other variables use the default test. ```R %R head(upset_test(movies, genres, tests=custom_tests)) ``` ```text [1] "year, length, budget, rating, votes, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, mpaa differ significantly between intersections" ``` -------------------------------- ### Use a Single Color for All Intersection Bars Source: https://krassowski.github.io/complex-upset/articles/Examples_R.html Apply a single, uniform color to all intersection bars by mapping 'fill' to a constant string (e.g., 'bars_color') and using 'scale_fill_manual' to define that color. The 'guide="none"' argument hides the legend for this fill. ```R set_size(8, 3) upset( movies, genres, base_annotations=list( 'Intersection size'=intersection_size( counts=FALSE, mapping=aes(fill='bars_color') ) + scale_fill_manual(values=c('bars_color'='blue'), guide='none') ), width_ratio=0.1 ) ```