krassowski / complex-upset

A library for creating complex UpSet plots with ggplot2 geoms
MIT License
469 stars 28 forks source link

Issue with fill colours for set sizes #188

Open spencsa opened 1 year ago

spencsa commented 1 year ago

**I would like to change the fill colours for the set size bars, to produce a stacked bar chart. I have developed my code based on the example coding here (https://krassowski.github.io/complex-upset/articles/Examples_R.html#change-position-and-add-fill).

However, I am unable to change the colours (either by stipulating exact colours or by using scale fill brewer palettes) - when I try other colours, the bars always come out as grey**

Code

upset <- upset(
  mm1, disease_names, width_ratio=0.2, height_ratio = 0.7,
  base_annotations=list(
    #Intersection size and annotations)
    'Intersection size'=intersection_size(mode = 'exclusive_intersection',
                                          text_mapping=aes(
                                            label=paste0(!!size, '\n(', round(!!get_size_mode('exclusive_intersection')/nrow * 100), '%', ')'
                                            )
                                          ), text=list( 
                                            size = 2.5), size=0.8, 
                                          mapping=aes(fill='bars_color')
    ) + ylab("Prevalence") 
    + scale_y_continuous(labels=function(x) paste0(x/nrow*100, "%"), breaks = seq(0,0.30*nrow,0.10*nrow), limits = c(0,0.30*nrow))
    + scale_fill_manual(values=c('bars_color'= NA), guide='none')
    + theme(plot.background = element_rect(color = 'white', fill = 'white'), 
            element_line(linewidth = 0, colour = 'white'))
  ), 
  #sets and set annotations
  encode_sets=FALSE,  # for annotate() to select the set by name disable encoding
  set_sizes=(
    upset_set_size(position='right', 
                   # add colours to the set sizes. THIS IS THE SECTION THAT DOESN'T SEEM TO BE WORKING 
                   geom=geom_bar(
                     aes(fill=mm_count2) + scale_colour_manual(values = c(
                       "0"= 'steelblue1',
                       "1"= 'firebrick1',
                       "2" = 'violetred3',
                       "3" = 'royalblue2',
                       "4" = 'firebrick', 
                       "5"= 'darkmagenta'))))
    + geom_text(aes(label=paste0(..count.., ' (', round(..count../nrow*100), '%', ')')), 
                stat='count', hjust =1.1, vjust = "middle",  size = 2.5, nudge_y = 80) 
    + scale_y_continuous(labels=function(x) paste0(x/nrow*100, "%"), breaks = seq(0,0.50*nrow,0.20*nrow), limits = c(0,0.50*nrow))
    # you can also add annotations on top of bars:
    + expand_limits(y=400)
    + theme(axis.text.x=element_text(angle=45))
    + ylab('Prevalence of individual diseases')
  ), 
  sort_sets = 'FALSE',
  sort_intersections_by=c('degree', 'cardinality'),
  ggtitle('Full data set'),
  sort_intersections = 'ascending',
  guides='over',
  # colour arrangements for intersection matrix (this is based on one of your previous threads)
  queries= c(
    query_by_degree(
      mm1, disease_names,
      mode=mode,
      matrix=intersection_matrix(
        geom=geom_point(shape='circle filled', size=10)),
      params_by_degree=list(
        '0'=list(color = 'steelblue1', fill='steelblue1'),
        '1'=list(color = 'firebrick1', fill='firebrick1'),
        '2'=list(color = 'violetred3', fill='violetred3'),
        '3'=list( color = 'royalblue2',fill='royalblue2'),
        '4'=list(color = 'firebrick',fill='firebrick'), 
        '5'=list(color = 'darkmagenta', fill='darkmagenta')), shared=list(
          only_components=c("intersections_matrix", "Intersection size", "Set size"),
          color='turquoise'))
  )
)


ComplexUpset version: x.x.x

R version details ```R ```
R session information ```R ```
krassowski commented 1 year ago

scale_colour_manual() appears to be in an incorrect position, it should not be added to aes() but after upset_set_size() as is scale_y_continuous() in your example.

spencsa commented 1 year ago

Thank you for the quick response. Really appreciate that.

Unfortunately the set sizes plot seems to have reverted to the default colours (the same as in your example here: https://krassowski.github.io/complex-upset/articles/Examples_R.html#change-position-and-add-fill.)

Code below. Any ideas?

upset(
  mm1, disease_names, width_ratio=0.2, height_ratio = 0.7,
  base_annotations=list(
    #Intersection size and annotations)
    'Intersection size'=intersection_size(mode = 'exclusive_intersection',
                                          text_mapping=aes(
                                            label=paste0(!!size, '\n(', round(!!get_size_mode('exclusive_intersection')/nrow * 100), '%', ')'
                                            )
                                          ), text=list( 
                                            size = 2.5), size=0.8, 
                                          mapping=aes(fill='bars_color')
    ) + ylab("Prevalence") 
    + scale_y_continuous(labels=function(x) paste0(x/nrow*100, "%"), breaks = seq(0,0.30*nrow,0.10*nrow), limits = c(0,0.30*nrow))
    + scale_fill_manual(values=c('bars_color'= NA), guide='none')
    + theme(plot.background = element_rect(color = 'white', fill = 'white'), 
            element_line(linewidth = 0, colour = 'white'))
  ), 
  #sets and set annotations
  encode_sets=FALSE,  # for annotate() to select the set by name disable encoding
  set_sizes=(
    upset_set_size(position='right', 
                   # add colours to the set sizes
                   geom=geom_bar(
                     aes(fill=mm_count2, x = group)))
    + scale_colour_manual(values = c(
      "0"= 'steelblue1',
      "1"= 'firebrick1',
      "2" = 'violetred3',
      "3" = 'royalblue2',
      "4" = 'firebrick', 
      "5"= 'darkmagenta')) 
    + geom_text(aes(label=paste0(..count.., ' (', round(..count../nrow*100), '%', ')')), 
                stat='count', hjust =1.1, vjust = "middle",  size = 2.5, nudge_y = 80) 
    + scale_y_continuous(labels=function(x) paste0(x/nrow*100, "%"), breaks = seq(0,0.50*nrow,0.20*nrow), limits = c(0,0.50*nrow))
    # you can also add annotations on top of bars:
    + expand_limits(y=400)
    + theme(axis.text.x=element_text(angle=45))
    + ylab('Prevalence of individual diseases')
  ), 
  sort_sets = 'FALSE',
  sort_intersections_by=c('degree', 'cardinality'),
  ggtitle('Full data set'),
  sort_intersections = 'ascending',
  guides='over',
  # colour arrangements for intersection matrix
  queries= c(
    query_by_degree(
      mm1, disease_names,
      mode=mode,
      matrix=intersection_matrix(
        geom=geom_point(shape='circle filled', size=10)),
      params_by_degree=list(
        '0'=list(color = 'steelblue1', fill='steelblue1'),
        '1'=list(color = 'firebrick1', fill='firebrick1'),
        '2'=list(color = 'violetred3', fill='violetred3'),
        '3'=list( color = 'royalblue2',fill='royalblue2'),
        '4'=list(color = 'firebrick',fill='firebrick'), 
        '5'=list(color = 'darkmagenta', fill='darkmagenta')), shared=list(
          only_components=c("intersections_matrix", "Intersection size", "Set size"),
          color='turquoise'))
  )
)
spencsa commented 1 year ago

worked it out. error in scale_fill_manual, where previously it was scale_colour_manual.

Thanks for your help!