R - 如何标记每个图形

时间:2018-01-28 13:38:09

标签: r graph label

我想标注我的图表,所以我不需要使用传奇,但我不知道该怎么做。

例如,使用这些数据我得到这3行

x = c(1:10)
y = x^2
z = x^3
w = 2*x + 7

plot(x,y,type="l", col="red")
lines(z, type="l", col="blue")
lines(w, type="l", col="green")

https://gyazo.com/a674a148c57e38160a502f3f51a41046

我想分别标记每个图,y,z和w。我希望它看起来像这样

https://gyazo.com/93a9e055a02f42fb61c3e1e438485dee

每个图形都有一个标签,因此不需要图例

我看了这个帖子 How can i label points in this scatterplot?

但这是一个散点图,我不确定如何为连续情节做这件事。

2 个答案:

答案 0 :(得分:1)

您可以设置绘图函数的xlabylab参数,以分别获取x和y轴上的标签:

plot(x,y,type="l", col="red", xlab="time", ylab="concentration")

然后使用text函数将标签放在各行上:

lines(z, type="l", col="blue")
lines(w, type="l", col="green")
text(10, 95, "R", cex = .8)
text(4, 100, "B", cex = .8)
text(10, 20, "G", cex = .8)

情节现在看起来像:

Plot with text labels

请参阅此处的文字功能https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html

答案 1 :(得分:0)

您还可以在locator()中使用text,然后点击所需的标签坐标,例如:

plot(x,y,type="l", col="red")
lines(z, type="l", col="blue")
lines(w, type="l", col="green")
text(locator(), labels = c("y", "z", "w"), col=c("red","blue","green"))

enter image description here