gpt4 book ai didi

r - 在 ggplot2 中使用 ggrepel

转载 作者:行者123 更新时间:2023-12-04 14:16:17 34 4
gpt4 key购买 nike

我有一个 geom_gene_arrow 图,我需要使用 ggrepel 来防止标签重叠。不幸的是,我无法让它工作并得到错误提示“找不到函数“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包裹。在我的 session 中,我没有任何问题来获取图表:

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

在这里使用 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

您可以在第一张图上看到, 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>)

关于r - 在 ggplot2 中使用 ggrepel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59997542/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com