Adjusting “Intersection size” to display the percentage of each group when the bars are filled
richardstoeckl opened this issue · 0 comments
Hello and thank you so much for this great package!
Objective
I am trying to create a plot, very similar to the ones shown in this part of the tutorial and mentioned in #103 . However, instead of the number (count), I want to show the percentage of each group in the stacked Intersection size bars (see images below). This way, one could easily see if a reduced amount of counts of a given intersection is just a consequence of a reduced amount of total entries of that group.
Sadly I was unable to do this with the sections mentioned in your reply here, as the !!aes_percentage(relative_to='group')
directive does not seem to work within the intersection_size()
function. And the text_mapping=aes(label=!!upset_text_percentage())
way of displaying the percentages does not split them by the stacked bar plot groups.
Is there a way to do this that I am overlooking?
Thank you so much
- RIchard
Code
library(tidyverse)
library(ComplexUpset)
# use data from tutorial
movies = as.data.frame(ggplot2movies::movies)
genres = colnames(movies)[18:24]
movies[genres] = movies[genres] == 1
movies[movies$mpaa == '', 'mpaa'] = NA
# for cleaner visualisation, split data into two groups ("released before 2000 and after")
movies = na.omit(movies) %>% mutate(pre2000=if_else(year<= 2000,"old","new"))
plot <- upset(movies,
genres,
width_ratio=0.1,
min_size = 0,
wrap = T,
mode = "distinct",
base_annotations=list(
'Intersection size'=intersection_size(
counts=F,
mapping=aes(fill=pre2000)
) +
stat_summary(fun=sum,
geom='text',
position=position_stack(vjust = 0.5),
aes(label=after_stat(y)), color = "white") +
theme(legend.position = "top", legend.title = element_blank())
),
annotations =list(
'Percentage of Group'=list(
aes=aes(x=intersection, fill=pre2000),
geom=list(
geom_bar(stat='count', position='fill', na.rm=TRUE),
geom_text(
aes(
label=!!aes_percentage(relative_to='group'),
group = pre2000
),
stat='count',
position=position_fill(vjust = .5)
),
scale_y_continuous(labels=scales::percent_format())
)
)
),
set_sizes=(
upset_set_size(
geom=geom_bar(
aes(fill=pre2000, x=group),
width=0.8, show.legend = FALSE
)
)
)
)
plot
Screenshot or illustration
This is the closest I could get with the code above:
And this is to show what I want to achieve:
Context (required)
ComplexUpset version: 1.3.3
R version details
$platform
[1] "x86_64-apple-darwin17.0"
$arch
[1] "x86_64"
$os
[1] "darwin17.0"
$system
[1] "x86_64, darwin17.0"
$status
[1] ""
$major
[1] "4"
$minor
[1] "2.2"
$year
[1] "2022"
$month
[1] "10"
$day
[1] "31"
$`svn rev`
[1] "83211"
$language
[1] "R"
$version.string
[1] "R version 4.2.2 (2022-10-31)"
$nickname
[1] "Innocent and Trusting"
R session information
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 14.1.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ComplexUpset_1.3.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.3 purrr_1.0.2 readr_2.1.4 tidyr_1.3.0
[9] tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] pillar_1.9.0 compiler_4.2.2 tools_4.2.2 lifecycle_1.0.3 gtable_0.3.4 timechange_0.2.0 pkgconfig_2.0.3
[8] rlang_1.1.2 cli_3.6.1 rstudioapi_0.15.0 patchwork_1.1.3 withr_2.5.2 generics_0.1.3 fs_1.6.3
[15] vctrs_0.6.4 ggplot2movies_0.0.1 hms_1.1.3 grid_4.2.2 tidyselect_1.2.0 glue_1.6.2 R6_2.5.1
[22] fansi_1.0.5 tzdb_0.4.0 farver_2.1.1 magrittr_2.0.3 scales_1.2.1 colorspace_2.1-0 labeling_0.4.3
[29] utf8_1.2.4 stringi_1.7.12 munsell_0.5.0 crayon_1.5.2