箭头标记ggplot2中的方向

时间:2011-09-16 22:47:42

标签: r 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")

输出 enter image description here

除箭头标签外,一切都很好。我希望标签位于箭头的末端,与箭头的方向相同。任何想法和/或解决方案。提前谢谢。

3 个答案:

答案 0 :(得分:5)

两种可能性:

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

最后是一个完全“三角化”的版本,它将使标签与箭头完全相同。 (符号(。)参数的相同性是反正切值的约定的结果。雅做你做的事情。):

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

答案 1 :(得分:1)

任何解决方案都可能需要一些人工操作定位。但最终在你的情况下,在没有调整调整的情况下绘制标签会更容易,然后稍微缩短每个箭头:

#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

请注意,段缩短必须与段的长度成比例地发生,因为箭头长度本身是固定的。

答案 2 :(得分:1)

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

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