【问题标题】:how to mark each data point in a plot with its label and user specified color? [duplicate]如何用标签和用户指定的颜色标记图中的每个数据点? [复制]
【发布时间】:2013-07-05 08:10:43
【问题描述】:

我有以下二维数据点,第一列是数据ID

ID    V1              V2
1   -9.2523712  1.751943612
2   -0.9799493  0.067998776
3   -0.9799493  0.067998776
4   3.2156859   1.088934239
5   3.4915597   1.097911743
6   3.4915597   1.097911743
7   -0.9799493  0.067998776
8   -0.9799493  0.067998776
9   -0.9799493  0.067998776
10  3.2156859   1.088934239

假设这个数组被命名为fit,我将这些点绘制为plot(fit[,2],fit[,3]) 但是,是否可以在图上用其ID 标记每个点?另外,对于一些特定的点,比如 ID 10,我想用红色标记它。我怎样才能在 R 中做到这一点?

【问题讨论】:

标签: r


【解决方案1】:

在你的 plot 命令之后,使用以下命令:

text(x=fit[, 2], y=fit[, 3], labels=fit[, 1])
points(x=fit[fit$ID==10, 2], y=fit[fit$ID==10, 3], col="red")

话虽如此,在 ggplot 中更好

【讨论】:

  • 在原始的plot 调用和points 调用中使用pch=19 会使事情变得更好。
【解决方案2】:

尝试使用letters[i] 抖动点来标记第 i 个点,并将其叠加在红色辐条显示多重性的向日葵图上。我们可以使用随机种子和抖动参数来调整它。从图中可以看出,点 a 没有重叠点,但在底部附近有五个重叠点(b、c、g、h、i),在右侧附近有四个重叠点(d、e、f、j) .

set.seed(19)
fitj <- transform(fit, V1 = jitter(V1, 10), V2 = jitter(V2), 10)
with(fit, sunflowerplot(V1, V2))
with(fitj, text(V1, V2, letters[1:10]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多