【问题标题】:R - How to label each graph [duplicate]R - 如何标记每个图[重复]
【发布时间】:2018-01-28 13:38:09
【问题描述】:

我想给我的图表添加标签,这样我就不需要使用图例了,但我不知道该怎么做。

例如,使用这些数据我得到这 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?

但这是一个散点图,我不知道如何为连续图做。

【问题讨论】:

  • 如果你使用ggplot2或lattice graphics,directlabels包可以自动定位它们。

标签: r graph label


【解决方案1】:

您可以设置plot函数的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)

情节现在看起来像:

在此处查看文本功能https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html

【讨论】:

  • 我在做类似的工作。你很快。
【解决方案2】:

您也可以在text 中使用locator() 并指向并单击所需的标签坐标,例如:

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

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 2020-12-31
    • 2023-01-25
    • 1970-01-01
    • 2014-08-26
    • 2021-11-10
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多