Search code examples
rggplot2upsetrextrafont

How to use custom font family in UpsetR plots?


I am trying to create set plot using the UpSetR package; however, I'd like to control the family of fonts. The ideal approach would be using theme function from ggplot2 but this is not supported at the moment by UpSetR (there's an open issue from 2016 on GitHub here) and results in NULL.

Example to create test plot:

# R version ---------------------------------------------------------------
# platform       x86_64-w64-mingw32          
# arch           x86_64                      
# os             mingw32                     
# system         x86_64, mingw32             
# status                                     
# major          3                           
# minor          5.1                         
# year           2018                        
# month          07                          
# day            02                          
# svn rev        74947                       
# language       R                           
# version.string R version 3.5.1 (2018-07-02)
# nickname       Feather Spray 

# Package versions --------------------------------------------------------
# Assumes packages are already installed
packageVersion(pkg = "extrafont") == "0.17"
packageVersion(pkg = "UpSetR") == "1.3.3"
packageVersion(pkg = "ggplot2") == "3.1.0"

# Load UpSetR -------------------------------------------------------------
library(UpSetR)
library(extrafont)
library(ggplot2)

# Example -----------------------------------------------------------------
movies <- read.csv( system.file("extdata", "movies.csv", package = "UpSetR"), header=T, sep=";" )

upset(data = movies, 
      order.by = "freq", 
      keep.order = TRUE,
      mainbar.y.label = "Example plot", 
      point.size = 4, 
      line.size = 1,
      sets.x.label = NULL)

Going forward, the ideal would be where UpSetR supports layers / + theme() function from ggplot2; however, the UpSetR is not able to use "+" "layer name" logic. For example, if + theme(text = element_text(family = "Times New Roman")) were added at the end of the call above, it would return NULL and produce no plot.

Can you please suggest any workaround (or customization of function in package) that would support custom fonts in the example plot above produced by UpSetR? Alternatively, is there a way to force default font family in all plots without specifying any family arguments manually?


Solution

  • One way of achieving this with another UpSet implementation would be:

    # install if needed
    if(!require(ggplot2movies)) install.packages('ggplot2movies')
    if(!require(devtools)) install.packages('devtools')
    devtools::install_github('krassowski/complex-upset')
    
    movies = as.data.frame(ggplot2movies::movies)
    genres = c('Action', 'Animation', 'Comedy', 'Drama', 'Documentary', 'Romance', 'Short')
    
    library(ComplexUpset)
    library(ggplot2)
    
    upset(
        movies, genres, min_size=45, width_ratio=0.1,
        themes=upset_default_themes(text=element_text(family='Times New Roman'))
    )
    

    Upset with Times New Roman

    Disclamer: I am the author of this implementation.