complex-upset icon indicating copy to clipboard operation
complex-upset copied to clipboard

Is there a way to set a ratio (height/width) for the whole plot?

Open paulimer opened this issue 1 year ago • 2 comments

Objective I filed this in help-request, but this might be a feature-request! The context is that in a shiny app, the upset plot will fill the entire available space, Screenshot from 2022-07-12 12-37-14

unless the width and height are specified in plotOutput(). Screenshot from 2022-07-12 12-36-37

However, I would like to let the user decide the plot's ratio. In other, custom ggplot2 plots, I managed it by adding + theme(aspect.ratio = input$ratio) (and changing the width and height relative to input$ratio in the call to ggsave). However, doing it here only impacts part of the upset plot (the intersection matrix). width_ratio and height_ratio only control the relative ratio of components.

Code

Using patchwork (I think?) works for the intersection matrix and the intersection size barplot, but the set sizes barplot space is "floating" far from them. Screenshot from 2022-07-12 13-45-46

p = (ComplexUpset::upset(
        data = plot_data() %>% as.data.frame(),
        intersect = names(all_results_choice())[contrast_sel_numeric()],
        name = "contrast",
        mode = input$int_type,
        width_ratio = 0.2,
        min_size = input$int_size,
        min_degree = input$min_degree,
        set_sizes = (
          ComplexUpset::upset_set_size() +
          geom_text(aes(label = ..count..), hjust = -0.3, stat = 'count', color = "white")
        )
        )
        ) & theme(aspect.ratio = 1)

Context (required)

ComplexUpset version: 1.3.3

R version details
> R.version
               _                           
platform       x86_64-redhat-linux-gnu     
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.3                         
year           2022                        
month          03                          
day            10                          
svn rev        81868                       
language       R                           
version.string R version 4.1.3 (2022-03-10)
nickname       One Push-Up                 

paulimer avatar Jul 12 '22 10:07 paulimer

My suggestion was going to be to wrap the plot in patchwork::wrap_elements call, but it does not suffice and the docs confirms that aspect.ratio is not respected:

Further you can still add title, subtitle, tag, and caption using the same approach as with normal ggplots (using ggtitle() and labs()) as well as styling using theme(). For the latter, only the theme elements targeting plot margins and background as well as title, subtitle, etc styling will have an effect.

I would suggest opening an issue on the patchwork repository asking whether theme(aspect.ratio= could be supported.

krassowski avatar Jul 13 '22 19:07 krassowski

A simple example for patchwork is comparing the result of ggplot(mtcars) + theme(aspect.ratio=2) with wrap_elements(ggplot(mtcars)) + theme(aspect.ratio=2).

krassowski avatar Jul 13 '22 19:07 krassowski