thackl / gggenomes

A grammar of graphics for comparative genomics
https://thackl.github.io/gggenomes/
Other
587 stars 64 forks source link

markdown expressions in geom_gene_text #191

Closed thackl closed 3 months ago

thackl commented 3 months ago

Got this via email

I’m contacting you because I have been using the package you developed (gggenomes) to built synteny plots, and my question is whether its possible to use markdown expressions in geom_gene_text layer.

I have already use the package ggtext to use italics and subscript for gene notation when using gggenes (from ggplot2) using the following syntax:

ggplot(data = data, aes(xmin = start, xmax = end, y = seqID, fill = phenotype, forward = strand)) + geom_gene_arrow() + geom_richtext(aes(x = end-(end-start)/2, y = seqID, label = subject.id)) + theme(strip.text.x = element_markdown())

I have already tried this in gggenomes but it doesn’t work with geom_gene_text().

Any suggestion?

thackl commented 3 months ago

Interesting question! Yes you can do it with geom_richtext as well. The main point is that when you use geoms from other packages with gggenomes, you need to explicitly set data= in the geom to the the right track (seqs(), genes(), links(), ...). Otherwise the geoms don't know which of the different track dataframes inside the gggenomes object to work on.

library(gggenomes)
library(tidyverse)
# install.packages("ggtext")
library(ggtext)

emale_genes <- emale_genes |>
  mutate(rich_name = ifelse(str_detect(name, "MCP"),
    "<span style='font-size:6pt; color:#0b996a'>*MCP*</span>", NA))

gggenomes(emale_genes, emale_seqs) +
  geom_gene() +
  geom_richtext(
    aes(x=(x+xend)/2, y=y+.4, label=rich_name),
    data=genes(), # << explicitly set the track where the underlying data comes from
    angle=45, fill=NA, label.color=NA)

image