简体   繁体   English

如何用R标记散点图上的点?

[英]How to label points on a scatterplot with R?

I am new to R and would like to know how to label data points on a scatterplot. 我是R的新手,想知道如何在散点图上标记数据点。 I tried the following code but I am getting error. 我尝试了以下代码,但我收到错误。

x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136,
      54210,48925,58937,76281,67789,69138,18026,90806,44893)
y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495,
      1232, 1746, 2410, 1791, 1706, 259, 1982, 836)

plot(x, y, main="Scatterplot ", xlab="xaxis ", ylab="yaxis ", pch=19)

names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R",
          "T","W","V","Y")

library(calibrate)
textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))

Error in text.default(X[posXposY], Y[posXposY], labs[posXposY], adj = c(-0.3,  :
plot.new has not been called yet

I don't understand about this error. 我不明白这个错误。 Please help me 请帮我

您可以使用text()函数轻松创建它。

text(x,y,labels=names)

You could do it in ggplot2 : 你可以在ggplot2做到这ggplot2

require(ggplot2)
d <- data.frame(x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136, 54210,48925,58937,76281,67789,69138,18026,90806,44893), y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495, 1232, 1746, 2410, 1791, 1706, 259, 1982, 836), names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y"))
ggplot(d, aes(x,y)) + geom_point() + geom_text(aes(label=names))

You might want the text labels not to be directly on top of the points, which you could accomplish by using the hjust or vjust arguments in the geom_text part. 您可能希望文本标签不直接位于点之上,您可以使用geom_text部分中的hjustvjust参数来完成这些geom_text

Your code works for me with: 您的代码适用于我:

> sessionInfo()
R version 2.14.2 Patched (2012-02-29 r58525)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=C                LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] calibrate_1.7 mgcv_1.7-13  

loaded via a namespace (and not attached):
[1] grid_2.14.2    lattice_0.20-0 Matrix_1.0-4   nlme_3.1-103  
[5] tools_2.14.2

Check you have an up-to-date R and version of calibrate and if not update them and try again. 检查您是否有最新的R和校准版本,如果没有更新,请重试。

It would be more natural to use the following ordering of your calls: 使用以下呼叫顺序会更自然:

> library(calibrate)
> names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R",
+           "T","W","V","Y")
> plot(x, y, main="Scatterplot ", xlab="xaxis ", ylab="yaxis ", pch=19)
> textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))

It shouldn't make any difference if the plot window produced by the plot() call is still open. 如果plot()调用生成的绘图窗口仍然打开,则不应该有任何区别。

You do not need the calibrate package. 您不需要校准包。 You can do: 你可以做:

text (x, y-50, names) 文字(x,y-50,姓名)

It does work for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM