Draw labels with split normal and italic faces in euler plot (eulerr)

716 Views Asked by At

The eulerr library produces a plot with the following code:

library(tidyverse)
library(eulerr)

matrix(data = c(T,T,T,F,T,F,T,F,T,T,T,F,F,F,T), ncol=3) %>%
  venn %>%
  plot(
    labels = c(
      "Left",
      "Right",
      "Oh, right"),
    main = expression("Oh,"~italic("right"))
       )

enter image description here

I need to have italic font in the labels, but only partially. I would like the third label to be formatted like the title: Oh, right.

I have tried a variety of permutations of paste, expression, bquote, substitute, while setting label = but to no avail.

Ideas?

1

There are 1 best solutions below

3
Ben On BEST ANSWER

You could try using getGrob and setGrob to replace the text after the venn diagram plot has been created.

library(eulerr)

mat <- matrix(data = c(T,T,T,F,T,F,T,F,T,T,T,F,F,F,T), ncol=3)

v <- venn(mat)

p <- plot(v,
  labels = c(
    "Left",
    "Right",
    "Oh, right"),
  main = expression("Oh,"~italic("right"))
)

p

gg <- getGrob(p, "tag.label.3")
gg[[1]] <- expression(bold("Oh,"~bolditalic("yes!")))
setGrob(p, "tag.label.3", gg)

venn plot with changed label

Edit: To find the grob that needs to be edited, you can use something like grid.ls:

library(grid)

grid.ls(p)

This will list the names of grobs in your plot, including tag.label.3:

euler.diagram
  main.grob
  canvas.grob
    diagram.grob.1
      fills.grob.1
      fills.grob.2
      fills.grob.3
      fills.grob.4
      fills.grob.5
      fills.grob.6
      fills.grob.7
      edges.grob
      tags
        tag.number.1
          tag.label.1
          tag.quantity.1
        tag.number.2
          tag.label.2
          tag.quantity.2
        tag.number.3
          tag.label.3
          tag.quantity.3
        tag.number.4
          GRID.null.1
          tag.quantity.4
        tag.number.5
          GRID.null.2
          tag.quantity.5
        tag.number.6
          GRID.null.3
          tag.quantity.6
        tag.number.7
          GRID.null.4
          tag.quantity.7

By trial/error, I found that tag.label.3 was the desired text.

In addition, looking at the eulerr package, you have:

# from tag-grobs.R in eulerr package
labels_grob <- textGrob(
  label,
  x = unit(x, "native"),
  y = unit(y, "native"),
  rot = labels$rot[data$labels_par_id],
  gp = labels$gp[data$labels_par_id],
  name = paste0("tag.label.", data$labels_par_id)
)

Where tag.label. is used as a prefix for the text labels.