![](/img/trans.png)
[英]Add arrow at the end of a `geom_segment` instead of pointing to the end of a `geom_segment` in `ggplot2`?
[英]How do I shift the geom_text labels to AFTER a geom_segment arrow in ggplot2?
我有一个使用 ggplot2 绘制的 NMDS 排序。 我已经使用geom_segment()
在顶部(来自vegan
的envfit()
函数)添加了环境向量,并使用geom_text()
(下面的代码)将相应的标签添加到与线段相同的坐标:
ggplot() +
geom_point(data = nmds.sites.plot, aes(x = NMDS1, y = NMDS2, col = greening), size = 2) +
labs(title = "Study Area",
col = "Sites") +
geom_polygon(data = hull.data, aes(x = NMDS1, y = NMDS2, fill = grp, group = grp), alpha = 0.2) +
scale_fill_discrete(name = "Ellipses",
labels = c("High", "Moderate", "Control")) +
xlim(c(-1, 1)) +
guides(shape = guide_legend(order = 1),
colour = guide_legend(order = 2)) +
geom_segment(data = env.arrows,
aes(x = 0, xend = NMDS1, y = 0, yend = NMDS2),
arrow = arrow(length = unit(0.25, "cm")),
colour = "black", inherit.aes = FALSE) +
geom_text(data = env.arrows, aes(x = NMDS1, y = NMDS2, label = rownames(env.arrows))) +
coord_fixed() +
theme_bw() +
theme(text = element_text(size = 14))
但是,由于标签对齐居中,标签的一部分有时会与箭头的末端重叠。 我想在箭头的末尾有文本 START。 在其他一些情况下,如果箭头朝上,它会推入文本的中间。 本质上,我希望能够同时看到箭头和文本。
我已经尝试使用geom_text_repel()
从ggrepel
方案,但似乎放置随意(并且还将从其他点或文本中的情节排斥(或只是不能做所有事情)。
[编辑] 下面是 NMDS 向量的坐标(这是上面示例代码中的env.arrows
对象):
NMDS1 NMDS2
Variable1 -0.46609087 0.27567532
Variable2 -0.21524887 -0.10128795
Variable3 0.59093184 0.03423775
Variable4 -0.00136418 0.46550043
Variable5 -0.30900813 -0.19659929
Variable6 0.53510347 -0.36387227
Variable7 0.66376246 -0.05220685
在下面的代码中,我们创建了一个径向移位函数来将标签移离箭头。 该位移包括一个常数加上一个附加位移,该位移随标签相对于 x 轴的角度的余弦的绝对值而变化。 这是因为theta
接近 0 或 180 度的标签与箭头的重叠长度更大,因此需要移动得比theta
接近 90 或 270 度的标签更远。
您可能需要稍微调整代码以将标签准确放置在您想要的位置。 此外,如果变量名称可以具有不同的宽度,您可能需要添加额外的调整。
附加说明:我已将变量名称转换为数据列。 您也应该对您的数据执行此操作,然后将该数据列映射到aes
的label
参数。 使用rownames(env.arrows)
作为标签到达ggplot
函数环境之外的外部数据框env.arrows
并破坏您在geom_text
的data
参数中提供的数据框的映射(尽管它可能不会导致在这种特殊情况下的问题)。
library(tidyverse)
library(patchwork)
# data
env.arrows = read.table(text=" var NMDS1 NMDS2
Variable1 -0.46609087 0.27567532
Variable2 -0.21524887 -0.10128795
Variable3 0.59093184 0.03423775
Variable4 -0.00136418 0.46550043
Variable5 -0.30900813 -0.19659929
Variable6 0.53510347 -0.36387227
Variable7 0.66376246 -0.05220685", header=TRUE)
# Radial shift function
rshift = function(r, theta, a=0.03, b=0.07) {
r + a + b*abs(cos(theta))
}
# Calculate shift
env.arrows = env.arrows %>%
mutate(r = sqrt(NMDS1^2 + NMDS2^2),
theta = atan2(NMDS2,NMDS1),
rnew = rshift(r, theta),
xnew = rnew*cos(theta),
ynew = rnew*sin(theta))
p = ggplot() +
geom_segment(data = env.arrows,
aes(x = 0, xend = NMDS1, y = 0, yend = NMDS2),
arrow = arrow(length = unit(0.25, "cm")),
colour = "black", inherit.aes = FALSE) +
geom_text(data = env.arrows, aes(x = NMDS1, y = NMDS2, label = var)) +
coord_fixed() +
theme_bw() +
theme(text = element_text(size = 14))
pnew = ggplot() +
geom_segment(data = env.arrows,
aes(x = 0, xend = NMDS1, y = 0, yend = NMDS2),
arrow = arrow(length = unit(0.2, "cm")),
colour = "grey60", inherit.aes = FALSE) +
geom_text(data = env.arrows, aes(x = xnew, y = ynew, label = var), size=3.5) +
coord_fixed() +
theme_bw() +
theme(text = element_text(size = 14)) +
scale_x_continuous(expand=expansion(c(0.12,0.12))) +
scale_y_continuous(expand=expansion(c(0.07,0.07)))
p / pnew
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.