Search code examples
rggplot2ggrepel

Using ggrepel in ggplot2


I have a geom_gene_arrow plot and I need to use ggrepel to prevent labels from overlapping. Unfortunately, I haven't been able to get it to work and get and error saying 'could not find function "geom_text_repel"'

Working example with only geom_text:

> ggplot(data, aes(xmin = start, xmax = end, y = genome, fill = colour, forward = direction)) +
     geom_gene_arrow() +
     geom_text(aes(x = end - ((end-start)/2), y = 1.2, label = gene, angle=90)) +
     facet_wrap(~ genome, scales = "free", ncol = 1) +
     theme_void()+
     xlab("")

And with the introduction of geom_text_repel it fails.

> ggplot(data, aes(xmin = start, xmax = end, y = genome, fill = colour, forward = direction)) +
     geom_gene_arrow() +
     geom_text_repel(aes(x = end - ((end-start)/2), y = 1.2, label = gene, angle=90)) +
     facet_wrap(~ genome, scales = "free", ncol = 1) +
     theme_void()+
     xlab("")

example data:

genome start end gene function colour direction
A 11638 12786 fadA6 ringdegradation green, -1
A 12798 13454 fadE30 cleavage, blue 1
A 13529 14341 fadD3 ringdegradation green -1

Any insight as to what I am doing wrong is greatly appreciated!


Solution

  • I think you need to load the ggrepel package. On my session, I do not have any issues to get the graph:

    library(ggplot2)
    library(ggrepel)
    library(gggenes)
    ggplot(data, aes(xmin = start, xmax = end, y = genome, fill = colour, forward = direction)) +
      geom_gene_arrow(arrowhead_height = unit(4, "mm"), 
                      arrowhead_width = unit(2, "mm"), arrow_body_height = unit(4, "mm")) +
      geom_text_repel(aes(x = end - ((end-start)/2), y = 1.2, label = gene, angle=90)) +
      facet_wrap(~ genome, scales = "free", ncol = 1) +
      theme_void()+
      xlab("")
    

    enter image description here

    And here using geom_text:

    library(ggplot2)
      library(ggrepel)
      library(gggenes)
      ggplot(data, aes(xmin = start, xmax = end, y = genome, fill = colour, forward = direction)) +
        geom_gene_arrow(arrowhead_height = unit(4, "mm"), 
                        arrowhead_width = unit(2, "mm"), arrow_body_height = unit(4, "mm")) +
        geom_text(aes(x = end - ((end-start)/2), y = 1.2, label = gene, angle=90)) +
        facet_wrap(~ genome, scales = "free", ncol = 1) +
        theme_void()+
        xlab("")
    

    enter image description here

    You can appreciate that on the first graph, geom_text_repel is working as label are not perfectly aligned with the half of the arrow.

    Reproducible example

    structure(list(genome = c("A", "A", "A"), start = c(11638L, 12798L, 
    13529L), end = c(12786L, 13454L, 14341L), gene = c("fadA6", "fadE30", 
    "fadD3"), `function` = c("ringdegradation", "cleavage,", "ringdegradation"
    ), colour = c("green,", "blue", "green"), direction = c(-1L, 
    1L, -1L)), row.names = c(NA, -3L), class = c("data.table", "data.frame"
    ), .internal.selfref = <pointer: 0x56276b4f1350>)