ggplot2中的箭头标签方向
我用这个 R
代码生成了双图:
set.seed(12345)
require(ggplot2)
AData <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1))
TData <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1))
i <- 2
j <- 3
p <- ggplot(data=AData, aes(AData[, i], AData[, j])) + geom_point() + theme_bw()
p <- p + geom_text(aes(data=AData, label=Glabel), size=3, vjust=1.25, colour="black")
p <- p + geom_segment(data = TData, aes(xend = TData[ ,i], yend=TData[ ,j]),
x=0, y=0, colour="black",
arrow=arrow(angle=25, length=unit(0.25, "cm")))
p <- p + geom_text(data=TData, aes(x=TData[ ,i], y=TData[ ,j], label=Tlabel), size=3, vjust=1.35, colour="black")
输出
除了箭头标签之外,一切都很好。我希望标签位于箭头的末端,且方向与箭头相同。任何想法和/或解决方案。提前致谢。
I produced biplot with this R
code:
set.seed(12345)
require(ggplot2)
AData <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1))
TData <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1))
i <- 2
j <- 3
p <- ggplot(data=AData, aes(AData[, i], AData[, j])) + geom_point() + theme_bw()
p <- p + geom_text(aes(data=AData, label=Glabel), size=3, vjust=1.25, colour="black")
p <- p + geom_segment(data = TData, aes(xend = TData[ ,i], yend=TData[ ,j]),
x=0, y=0, colour="black",
arrow=arrow(angle=25, length=unit(0.25, "cm")))
p <- p + geom_text(data=TData, aes(x=TData[ ,i], y=TData[ ,j], label=Tlabel), size=3, vjust=1.35, colour="black")
Output
Everything is fine except the arrows labels. I'd like to have the labels at the end of the arrows and in the same direction as arrows. Any idea and/or solution. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两种可能性:
第一种使标签与箭头的距离随“强度”而变化,而第二种在图形绘制比例上保持固定距离(可能是首选解决方案,如下所示)。
最后是一个完全“三角化”的版本,它将使标签与箭头完全相同。 (sign(.) 参数的相同性是反正切值约定的结果。Ya do what ya gatta do.):
Two possibilities:
First one leaves labels away from the arrows a distance that varies with the "strengths" while the second one a fixed distance on the graphics plotting scale (probably the preferred solution and the one illustrated below).
And finally a fully "trigonometricized "version that will get the labels exactly in the same vector as the arrows. (The sameness of the sign(.) argument is a result of the conventions for arctangent values. Ya do what ya gatta do.) :
任何解决方案都可能需要一些手动定位。但最终在您的情况下,在不进行 vjust 调整的情况下绘制标签会更容易,然后稍微缩短每个箭头:
由于箭头长度本身是固定的,因此线段的缩短必须与线段的长度成一定比例。
Any solution will likely require some manual fussing with positioning. But ultimately in your case it will be much easier to plot the labels without the vjust adjustments and then shorten each of the arrows slightly:
Note the segment shortening had to happen somewhat proportionally to the length of the segment, since the arrow head lengths themselves are fixed.
标签方向为箭头的更好解决方案:
A better solution with label direction as arrows: