1

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?

gofraidh
  • 673
  • 8
  • 26
  • Did you set up the usable fonts based on the extrafont docs? I've mostly switched to using showtext but I remember setting up fonts with extrafont to be a little finicky – camille Feb 12 '20 at 15:02
  • @camille Yes, there's not an issue with that. I can change the family of font in any plots produced by ```ggplot()``` but I can't change them in plots produced by ```upset()``` as the function does not seem to support any way of adding different fonts. – gofraidh Feb 12 '20 at 15:14
  • @camille I am looking at the package you mentioned - showtext. Do you please know if it allows to change defaults font in any plot produced without me having to manually specify ```family``` argument? If I could overwrite default font option and force all plots to use only a specific fonts then that could be a potential solution... – gofraidh Feb 12 '20 at 15:20
  • I'm not familiar with upsetR specifically. But when I'm doing multiple plots with ggplot, I'll usually set up a theme with the font I want, then call `theme_set` to make it the default theme for that session/RMarkdown document. That's not specific to showtext, that's a ggplot function – camille Feb 12 '20 at 15:28
  • @camille Thank you. Although UpSetR creates plots with support of ggplot2 somewhere in backend, it (strangely) does not allow the + ```theme``` function (nor it seems to supports adding layers of graphics with +). To me this really is an issue with the UpSetR...otherwise I'd be happy doing what you do. – gofraidh Feb 12 '20 at 15:37
  • 1
    You've got a `+ theme` line right there...you mean that has no effect, even for theme elements besides font family? Probably more work than you want, but I just found [this blog post](http://research.libd.org/rstatsclub/post/hacking-our-way-through-upsetr/) on rewriting some of the ggplot calls that `upset` makes – camille Feb 12 '20 at 15:38
  • @camille I have edited the above question because I guess the ```+ theme``` line made it appear as if that produced anything. Hope this made it more clear. Okay, that blog post is really useful! I think I will have to do my own UpSetR repo and modify the package itself. Thanks! – gofraidh Feb 12 '20 at 15:50
  • 1
    My ggplot-based re-implementation of https://github.com/krassowski/complex-upset is addressing this issue. Would it be worth posting an example as an answer? – krassowski Mar 10 '20 at 17:59
  • @krassowski looks good to me! The "hack" I've implemented was to "abuse" the fact that UpSetR relies on ggplot2. I've discovered that if you modify the `"family"` argument of `theme_update(...)` and add it as layer, then it (surprisingly) updates the fonts too. Although that doesn't seem like intended behaviour (on side of the UpSetR) to me. Having that said, I'd be happy to see your example, in my opinion the answer requires updating the package anyway (the documentation of your package is good, but if you post it the example, I'll mark it as an answer). – gofraidh Mar 10 '20 at 23:27

1 Answers1

4

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.

Community
  • 1
  • 1
krassowski
  • 13,598
  • 4
  • 60
  • 92
  • The font works font but I'm getting slightly different plot with numbers above the bars, which have a strange position – MLEN Apr 10 '20 at 09:00
  • have a look at https://krassowski.github.io/complex-upset/articles/Examples_R.html#counts – krassowski Apr 10 '20 at 09:44
  • Looks like I have some interesting reading over the weekend, thanks! – MLEN Apr 10 '20 at 10:49