在 ggplot2 中使用 ggrepel

Using ggrepel in ggplot2

我有一个 geom_gene_arrow 图,我需要使用 ggrepel 来防止标签重叠。不幸的是,我无法让它工作并得到错误提示 'could not find function "geom_text_repel"'

只有 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("")

并且随着 geom_text_repel 的引入它失败了。

> 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("")

示例数据:

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

非常感谢任何关于我做错了什么的见解!

我认为您需要加载 ggrepel 包。在我的会话中,获取图表没有任何问题:

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("")

这里使用 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("")

您可以看到,在第一张图中,geom_text_repel 正常工作,因为标签与箭头的一半没有完全对齐。

可重现的例子

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>)