rggplot2ggalluvial

Adding markdown formatted text to ggalluvial plots


I wish to add formatting to some of the labels in various columns of the following alluvial plot:

library(ggplot2)
library(ggalluvial)

dataset <- data.frame(Gene = c("A1", "B2", "C1", "DNA repair", "Q3"),
Type.of.mutation = c("somatic", "somatic", "somatic", "germline", "somatic"), 
Drug = c("A", "B", "C", "D", "E"), 
Response = c("Responsive", "Responsive", "Responsive", "Resistant", "Responsive"), 
weight = c(7L, 4L, 9L, 8L, 10L))

p <- (ggplot(dataset,aes(y=weight, axis2=Gene, axis1=Type.of.mutation, axis3=Drug)) + 
geom_alluvium(aes(fill=Response), width=1/2, knot.pos=0.2, reverse=TRUE) +
geom_stratum(width=1/2, fill="white", color="black", alpha=0.5) +
geom_label(stat="stratum", aes(label=after_stat(stratum)), size=3, label.size=NA,alpha=0) +
scale_x_discrete(limits=c("Mutation type", "Gene / Function", "Drug")) +
theme_bw() +
theme(
  axis.text.x=element_text(size=12, color="black"),
  axis.title.y=element_blank(), 
  axis.text.y=element_blank(),
  axis.ticks.y=element_blank(),
  axis.ticks.x=element_blank(),
  panel.grid.major=element_blank(), 
  panel.grid.minor=element_blank(),
  panel.border = element_blank(),
  panel.background = element_blank(),
  plot.title=element_text(hjust=0.5)
) +
theme(legend.position="bottom") +
ggtitle( paste0("Clinically-actionable targets" ) ))
print(p)

This currently generates the following alluvial plot:

Alluvial plot

I have tried replacing the geom_label() call with ggtext::geom_richtext() from the ggtext package (and adding markdown to the relevant labels), but this makes the labels disappear.

Is there a way to selectively add text formatting to some (but not all) of the stratum labels? If all else fails, I could add the labels in afterwards, but I'd prefer an automated version in case I needed to change the data going into the plot.


Solution

  • The issue with using ggtext::geom_richtext is that you have set alpha=0. Looks like alpha is applied to the fill only in case of geom_label whereas for ggtext::geom_richtext it is also applied to the text color. Hence, to fix your issue drop alpha=0 and set fill=NA to make the fill color transparent. As an example of markdown formatting I added an ifelse to make the A labels bold.

    library(ggplot2)
    library(ggalluvial)
    
    p <- (ggplot(dataset, aes(y = weight, axis2 = Gene, axis1 = Type.of.mutation, axis3 = Drug)) +
      geom_alluvium(aes(fill = Response), width = 1 / 2, knot.pos = 0.2, reverse = TRUE) +
      geom_stratum(width = 1 / 2, fill = "white", color = "black", alpha = 0.5) +
      ggtext::geom_richtext(
        stat = "stratum",
        aes(label = after_stat(
          ifelse(grepl("^A", stratum), paste0("**", stratum, "**"), as.character(stratum))
        )),
        size = 3,
        label.size = NA,
        fill = NA
      ) +
      scale_x_discrete(limits = c("Mutation type", "Gene / Function", "Drug")) +
      theme_bw() +
      theme(
        axis.text.x = element_text(size = 12, color = "black"),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        plot.title = element_text(hjust = 0.5)
      ) +
      theme(legend.position = "bottom") +
      ggtitle(paste0("Clinically-actionable targets")))
    print(p)