ggplot2中的箭头标签方向

发布于 2024-12-04 18:40:49 字数 950 浏览 0 评论 0原文

我用这个 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
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沦落红尘 2024-12-11 18:40:49

两种可能性:

p <- p + geom_text(data=TData, aes(x=1.2*TData[ ,i], 
                                   y=1.2*TData[ ,j], 
                  label=Tlabel), size=3, vjust=0, colour="black")
p
#-----------------------------------
p <- p + geom_text(data=TData, aes(x=TData[ ,i] +.1*sign(TData[ ,i]), 
                                   y=TData[ ,j]+.1*sign(TData[ ,j]), 
                  label=Tlabel), size=3, vjust=0, colour="black")
p

第一种使标签与箭头的距离随“强度”而变化,而第二种在图形绘制比例上保持固定距离(可能是首选解决方案,如下所示)。

Second version as image

最后是一个完全“三角化”的版本,它将使标签与箭头完全相同。 (sign(.) 参数的相同性是反正切值约定的结果。Ya do what ya gatta do.):

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] +.1*cos(atan(TData[ ,j]/TData[ ,i]))*sign(TData[ ,i]), 
                y=TData[ ,j] +.1*sin(atan(TData[ ,j]/TData[ ,i]))*sign(TData[ ,i]), 
            label=Tlabel), size=3, vjust=0, colour="red")

Two possibilities:

p <- p + geom_text(data=TData, aes(x=1.2*TData[ ,i], 
                                   y=1.2*TData[ ,j], 
                  label=Tlabel), size=3, vjust=0, colour="black")
p
#-----------------------------------
p <- p + geom_text(data=TData, aes(x=TData[ ,i] +.1*sign(TData[ ,i]), 
                                   y=TData[ ,j]+.1*sign(TData[ ,j]), 
                  label=Tlabel), size=3, vjust=0, colour="black")
p

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

Second version as image

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

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] +.1*cos(atan(TData[ ,j]/TData[ ,i]))*sign(TData[ ,i]), 
                y=TData[ ,j] +.1*sin(atan(TData[ ,j]/TData[ ,i]))*sign(TData[ ,i]), 
            label=Tlabel), size=3, vjust=0, colour="red")
执笏见 2024-12-11 18:40:49

任何解决方案都可能需要一些手动定位。但最终在您的情况下,在不进行 vjust 调整的情况下绘制标签会更容易,然后稍微缩短每个箭头:

#This is the ad-hoc part to tinker with
l <- 1 - (0.05 * sqrt(1/with(TData,A^2 + B^2)))

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

#Scale the segments back slightly
p <- p + geom_segment(data = TData, aes(xend = TData[ ,i] * l, yend=TData[ ,j] * l),
              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, colour="black")

由于箭头长度本身是固定的,因此线段的缩短必须与线段的长度成一定比例。

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:

#This is the ad-hoc part to tinker with
l <- 1 - (0.05 * sqrt(1/with(TData,A^2 + B^2)))

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

#Scale the segments back slightly
p <- p + geom_segment(data = TData, aes(xend = TData[ ,i] * l, yend=TData[ ,j] * l),
              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, colour="black")

enter image description here

Note the segment shortening had to happen somewhat proportionally to the length of the segment, since the arrow head lengths themselves are fixed.

压抑⊿情绪 2024-12-11 18:40:49

标签方向为箭头的更好解决方案:

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
            , angle = (180/pi) * atan(TData[ ,j]/TData[ ,i])
            , hjust = (1 - 2 * sign(TData[ ,i])) / 2)
      , size=3
      , colour="black")

print(p)

在此处输入图像描述

A better solution with label direction as arrows:

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
            , angle = (180/pi) * atan(TData[ ,j]/TData[ ,i])
            , hjust = (1 - 2 * sign(TData[ ,i])) / 2)
      , size=3
      , colour="black")

print(p)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文