Home > Mobile >  R: geom_venn how to change font
R: geom_venn how to change font

Time:10-25

Is there way to change font style when drawing a venn diagram using geom_venn function? I have tried theme(text=element_text(size=16, family="Comic Sans MS")) but for some reason it doesn't work

CodePudding user response:

I don't know, which library (think, that ggvenn) do you use, but as you can see, changing of the font doesn't affect the diagram. Only axis, legend etc.

Examples:

library(ggvenn)
d <- tibble(value   = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
             `Set 1` = c(T, F, T, T, F, T, F, T, F,  F,  F),
             `Set 2` = c(T, F, F, T, F, F, F, T, F,  F,  T),
             `Set 3` = c(T, T, F, F, F, F, T, T, F,  F,  F),
             `Set 4` = c(F, F, F, F, T, T, F, F, T,  T,  F)) 

ggplot(d, aes(A = `Set 1`, B = `Set 2`))  
      geom_venn()  
      theme(text = element_text(size=20,  family="Comic Sans MS"))   
      coord_fixed() 

enter image description here

I tried also ggVennDiagram

 library(ggVennDiagram)
 genes <- paste("gene",1:1000,sep="")
 set.seed(20210419)
 x <- list(A=sample(genes,300),
           B=sample(genes,525))

 ggVennDiagram(x)   
          scale_fill_gradient(low="blue",high = "red")  
          theme(text = element_text(size=20,  family="Comic Sans MS"), 
                axis.text = element_text(size=20,  family="Comic Sans MS"),)

enter image description here

CodePudding user response:

I'm not sure this is possible directly within ggvenn. The following function will allow you to change the font of the text labels after your plot is created though:

venn_font <- function(p, font)
{

  grep_grob <- function(gt, lab){
    which(sapply(gt, function(x) grepl(lab, x$name)))
  }
  
  p2 <- ggplot_gtable(ggplot_build(p))
  mygrobs <- p2$grobs
  panel_grob <- mygrobs[[grep_grob(mygrobs, "panel")]]
  venn_grob <- panel_grob$children[[grep_grob(panel_grob$children, "venn")]]
  text_grob <- venn_grob$children[grep_grob(venn_grob$children, "text")]
  text_grob <- do.call(grid::gList, 
                       lapply(text_grob, 
                              function(x) {x$gp$fontfamily <- font; 
                                           x}))
  venn_grob$children[grep_grob(venn_grob$children, "text")] <- text_grob
  panel_grob$children[[grep_grob(panel_grob$children, "venn")]] <- venn_grob
  mygrobs[[grep_grob(mygrobs, "panel")]] <- panel_grob
  p2$grobs <- mygrobs
  grid::grid.newpage()
  grid::grid.draw(p2)
  
}

So, testing it out (with thanks to manro for the example data):

library(ggvenn)

d <- tibble(value   = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
             `Set 1` = c(T, F, T, T, F, T, F, T, F,  F,  F),
             `Set 2` = c(T, F, F, T, F, F, F, T, F,  F,  T),
             `Set 3` = c(T, T, F, F, F, F, T, T, F,  F,  F),
             `Set 4` = c(F, F, F, F, T, T, F, F, T,  T,  F)) 

p <- ggplot(d, aes(A = `Set 1`, B = `Set 2`))  
      geom_venn()  
      coord_fixed() 

venn_font(p, font = "Comic Sans MS")

enter image description here

  • Related