r - R 中的标注文本

标签 r text graphics plot callout

我想在我的 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/

相关文章:

r - 在 Mac OS X Maverick 上启动 Rserve 时出错

r - 空间权重 : asymmetric adjacency matrix?

css - 将容器边缘的文本淡入透明

search - 查找大文本中重复次数最多的短语

flash - Flash 可以显示 UTF-8 文本吗?

algorithm - Reinhard 色调映射 2002

java - 将抽象 Graphics 对象传递给paintComponent()

mysql - 使用 sqldf 在 R 中对重复项进行编号

mysql - 通用函数 ‘dbListFields’ 、类 “numeric” 的值无效,应为 “character”

algorithm - 存储形状(或线条)数据的有效数据结构是什么