gpt4 book ai didi

r - R 中的标注文本

转载 作者:行者123 更新时间:2023-12-04 11:59:48 27 4
gpt4 key购买 nike

我想在我的 R 图中添加标注文本。例如,我有一些情节:

x <- seq(0, 2, by=0.1)
y <- x*x
plot(x, y, type="l")

enter image description here

我想添加一些这样的标注文本:

enter image description here

有一些标准的方法吗?

最佳答案

看看?text (和 ?arrows )。

x <- seq(0, 2, by=0.1)
y <- x*x
plot(x, y, type="l")

text(0.25, 2, "Some text", pos=3)
arrows(0.25, 2, 1, 1)

calloutplot1

更新 :

你可以结合 text , linesarrows变成一个小函数:
x <- seq(0, 2, by=0.1)
y <- x*x
plot(x, y, type="l")

# x0, y0: coordinates of text; see ?text
# x1, y1: coordinates to which the arrows are drawn; see ?arrows
# labels: text
# vOffset/hOffset: vertical/horizontal offset
callout <- function(x0, y0, x1, y1, labels, vOffset=0.25, hOffset=0.25) {
## fetch labels width/height
w <- strwidth(labels)
w2 <- w/2
h <- strheight(labels)
h2 <- h/2

## draw text
text(x0, y0, labels=labels)

## calulate arrow starting point/line end point
x01 <- x0+w2*(1+hOffset)
y01 <- y0-h2*(1+vOffset)

## draw horizontal lines
for (i in seq(along=x0)) {
lines(c(x0[i]-w2[i], x01[i]), c(y01[i], y01[i]))
}

## draw arrows
arrows(x0=x01, y0=y01, x1=x1, y1=y1)
}

callout(c(0.25, 0.25), c(2, 3), c(1, 1.5), c(1, 2.25),
c("Some text", "Some other text"))

calloutplot2

关于r - R 中的标注文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389465/

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