4

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!

Lauren
  • 77
  • 1
  • 6
  • Did you load `ggrepel` ? Also, in your `ggplot` code, there is mention of `start`,`end` and `genome` variables that are not present in your example. Can you provide a reproducible example of your dataset ? (please follow this tutorial: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Jan 31 '20 at 03:41
  • Apologies! I just added in the missing columns. – Lauren Jan 31 '20 at 03:48
  • Also yes, I loaded ggrepel and it stated that it installed correctly. – Lauren Jan 31 '20 at 03:56
  • Weird... when you installed `ggrepel`, you did not notice `installation of package ‘ggrepel'had non-zero exit status` ? The common error for a function not found is the package not loaded (such as `library(ggrepel)` before running the code) – dc37 Jan 31 '20 at 04:01
  • I just found the error - I am ashamed to admit I simply didn't have library(ggrepel) in my preamble... haha. Thank you for your help. – Lauren Jan 31 '20 at 04:31

1 Answers1

3

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>)
dc37
  • 15,840
  • 4
  • 15
  • 32
  • You're right! I had the package installed but didn't have library(ggrepel) in my preamble... – Lauren Jan 31 '20 at 04:32