【问题标题】:How to label individual points with vectors in R如何在 R 中用向量标记单个点
【发布时间】:2020-05-16 19:26:16
【问题描述】:

总的来说,我对编码和 R 很陌生。我试图弄清楚如何将图创建为单个点和向量。对于这两个选项,我应该得到相同的结果,但我似乎无法弄清楚在使用向量时如何关联点的标签。

Here's the table I was given

这是我的个人绘图代码和plot

plot(
        x = NULL,
        xlim = c(0, 8),
        ylim = c(0, 10),
        main = "Problem 3a- Individual Points Fuction",
        xlab = "x",
        ylab = "y",
        las = 1
    )

text( 0.6, 7.5, "A" )
points( 1, 7, pch = 19, cex = 3, col = "navy" )

text( 3.4, 2.5, "B" )
points( 4, 3, pch = 15, cex = 6, col = "blueviolet" )

text( 5.6, 4.0, "C" )
points( 6, 5, pch = 17, cex = 4, col = "firebrick2" )

text( 1.6, 1.5, "D" )
points( 2, 2, pch = 18, cex = 5, col = "cyan3" )

text( 6.8, 3.5, "E" )
points( 7, 4, pch = 16, cex = 2, col = "seagreen3" )

这是我的向量方法代码,带有plot

plot(
        x = NULL,
        xlim = c(0, 8),
        ylim = c(0, 10),
        main = "Problem 3b- Vector Points Fuction",
        xlab = "x",
        ylab = "y",
        las = 1
    )

points(

x = c(1, 4, 6, 2, 7),

y = c(7, 3, 5, 2, 4),

pch = c(19, 15, 17, 18, 16),

cex = c(3, 6, 4, 5, 2),

col = c("navy", "blueviolet", "firebrick2", "cyan3", "seagreen3"),

)

我似乎无法弄清楚如何标记向量上的点,并将其标记在某些坐标处。我已经尝试将 Text = ("A", "B", etc) 以及尝试将其设为向量 (text = c("A",etc),但我不断收到错误。任何建议和资源将不胜感激。

【问题讨论】:

    标签: r vector label scatter-plot


    【解决方案1】:

    您可以使用text 函数,如下所示。我添加了xDisp 变量来轻松设置标签位置(如果需要,您可以添加yDisp 变量以及垂直位置)。

    xDisp = -0.5
    
      plot(
        x = NULL,
        xlim = c(0, 8),
        ylim = c(0, 10),
        main = "Problem 3b- Vector Points Fuction",
        xlab = "x",
        ylab = "y",
        las = 1
      )
    
      points(
    
        x = c(1, 4, 6, 2, 7),
    
        y = c(7, 3, 5, 2, 4),
    
        pch = c(19, 15, 17, 18, 16),
    
        cex = c(3, 6, 4, 5, 2),
    
        col = c("navy", "blueviolet", "firebrick2", "cyan3", "seagreen3")
    
      )
    
      text(
    
        x = c(1+xDisp, 4+xDisp, 6+xDisp, 2+xDisp, 7+xDisp), y = c(7, 3, 5, 2, 4), labels = c("A","B","C","D","E")
    
      )
    

    【讨论】:

    • 非常感谢!这更有意义,也更容易阅读!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2021-08-16
    • 1970-01-01
    • 2020-06-27
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多