rggplot2superscriptvenn-diagramggvenn

How to add superscripts to each circle of a venn diagram using ggvenn?


I am trying to add superscripts to each circle of one venn diagram. I am using the R package ggvenn and these have been my attempts.

#Normal situation
set.seed(20190708)
genes <- paste("gene",1:1000,sep="")
x <- list(
  A_vs_X = sample(genes,300), 
  B_vs_P = sample(genes,525)
)
ggvenn::ggvenn(x)

Output:

enter image description here

However, I want to generate something like this:

enter image description here

I know that in order to get each of the subscripts, I need to do this:

expression('A'^(tt/tx))
expression('X'^(tw/ga))
expression('B'^(tt/tx))
expression('P'^(tw/ga))

Although for some reason if I want to paste two, it doesn't work (I don't know why)

paste0(expression('A'^(tt/tx)), expression('X'^(tw/ga)))

In any case, I have tried the following with ggvenn but the result is not what I am expecting (it appears "title1" and "title2" instead of the subscripts that I created:

A <- expression('A'^(tt/tx))
X <- expression('X'^(tw/ga))
title1 <- paste0(A, " vs ", X)

B <- expression('B'^(tt/tx))
P <- expression('P'^(tw/ga))
title2<- paste0(B, " vs ", P)
x <- list(
  title1 = sample(genes,300), 
  title2 = sample(genes,525)
)
ggvenn::ggvenn(x)

enter image description here

I have also tried this, but I get the following error:

x <- list(
  expression("A"^(tt/tx) "vs X"^(tw/ga)) = sample(genes,300), 
  expression("B"^(tt/tx) "vs P"^(tw/ga)) = sample(genes,525)
)
Error: unexpected string constant in:
"x <- list(
  expression("A"^(tt/tx) "vs X""

Does anyone know how to do it?

Thanks in advance


Solution

  • One hacky option would be to use ?plotmath character strings and manipulate the ggplot object returned by ggvenn, i.e. set parse=TRUE in the geom_text layer which under the hood is used to add the "titles" which for your example data is the third layer of the ggplot object. Additionally I have set the vertical alignment to .5.

    # Normal situation
    set.seed(20190708)
    genes <- paste("gene", 1:1000, sep = "")
    x <- list(
      "A^{(tt/tx)}~vs~X^{(tw/ga)}" = sample(genes, 300),
      "B^{(tt/tx)}~vs~P^{(tw/ga)}" = sample(genes, 525)
    )
    p <- ggvenn::ggvenn(x)
    
    p$layers[[3]]$aes_params$vjust <- .5
    p$layers[[3]]$geom_params$parse <- TRUE
    
    p