我使用以下命令绘制两条线
plot(x, y, type = "l", color = "red")和
points(x2, y2, type = "l", color = "blue")我希望能够在每一行旁边添加一个标签(而不是图例)。我非常确定在http://directlabels.r-forge.r-project.org/中使用这个包是可能的。
然而,我找不到一种简单的方法来做到这一点。
发布于 2010-11-17 01:32:43
您可以通过点击的方式在text()中使用locator()。
y <- rnorm(100, 10)
y2 <- rnorm(100, 20)
x <- 1:100
plot(x, y, type = "n", ylim = c(0, 40), xlim = c(0, 120))
lines(x, y)
lines(x, y2, col = "red")
text(locator(), labels = c("red line", "black line)"))

发布于 2010-11-17 09:54:28
除了使用locator(),您还可以将标签坐标设为数据的函数。例如,利用罗曼的演示:
text(x=rep(max(x)+3, 2), y=c(mean(y), mean(y2)), pos=4, labels=c('black line', 'red line'))发布于 2013-02-08 23:05:12
locator()是一种通过单击现有图形来获取坐标的交互式方法。
以下是有关如何使用locator()在图表上查找标签的正确坐标的说明。
第1步:绘制图形:
plot(1:100)第2步:在控制台中键入以下内容:
coords <- locator()第3步:在绘图上单击一次,然后单击绘图左上角的Stop .. Stop Locator (这会将控制权返回给R控制台)。
第4步:查找返回的坐标:
coords
$x
[1] 30.26407
$y
[1] 81.66773步骤5:现在,您可以使用以下坐标向现有绘图添加标签:
text(x=30.26407, y=81.66773,label="This label appears where I clicked")或
text(x=coords$x, y=coords$y,label="This label appears where I clicked")结果如下:

您会注意到,标签显示在您单击的位置的中心。如果标签的第一个字符出现在您单击的位置,效果会更好。要查找正确的参数,请参阅text的帮助,然后添加参数pos=4
text(x=30,y=80,pos=4,label = "hello")备注:
legend()绘制标签(这会在标签周围绘制一个通常看起来更好的方框)。ggplot2是生成图形的黄金标准。https://stackoverflow.com/questions/4196964
复制相似问题