27

I am new to R and would like to know how to label data points on a scatterplot. 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

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
lara
  • 503
  • 5
  • 10
  • 15
  • 1
    The error message sounds like your plot is not open any more when you call `textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))`. Try to call your plot statement again before using the labels. – Sophia Feb 29 '12 at 13:39
  • @sophia, Thanks for your comment. I tried. But still I am getting error – lara Feb 29 '12 at 15:19
  • Just out of interest, since I'm not familiar with `calibrate`, is there something `textxy` does that the basic `text` tool for labelling points doesn't do? – Carl Witthoft Feb 29 '12 at 20:07
  • @lara are you closing the plot before calling textxy? Do *not* close the plot window since that gives exactly the error you have when you attempt any annotating functions. – AdamO May 10 '13 at 18:02

4 Answers4

48

You can easily create this by using the text() function.

text(x,y,labels=names)
gentimouton
  • 145
  • 3
  • 11
TheRimalaya
  • 4,232
  • 2
  • 31
  • 37
16

You could do it in 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.

Dan M.
  • 1,526
  • 1
  • 12
  • 17
  • Thank you for your Answer. I tried your code. but getting this error.Error in get(x, envir = this, inherits = inh)(this, ...) : attempt to apply non-function – lara Mar 01 '12 at 07:09
  • I am not sure what is causing that, but this might help: [Error when trying to load library(ggplot2) in R](http://stackoverflow.com/questions/6066130/error-when-trying-to-load-libraryggplot2-in-r) – Dan M. Mar 01 '12 at 14:16
7

You do not need the calibrate package. You can do:

text (x, y-50, names)

It does work for me.

Tomaquina
  • 71
  • 1
  • 1
  • And why this answer is better than other one which has been accepted by OP's? – bcesars Mar 27 '15 at 14:07
  • 4
    Because it doesn't require any extra dependencies – fny Jan 23 '18 at 22:16
  • 1
    I alos like this a bit better than TheRimalaya's solution because of the offset (though the offset that you choose should be based on the scale of you chart. -50 probably works for the question at hand) – Brian Underwood Jan 26 '18 at 19:26
6

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.

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.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Thank you for your answer. I updated R but still I am getting same error. I am using windows. – lara Feb 29 '12 at 13:52
  • @lara did you update the package too? Or was it already the latest version? – Gavin Simpson Feb 29 '12 at 13:54
  • Then I am stumped; your code works for me using your out-of-order code and the more ordered version I put in the edit to my Answer. It is now time to try this in a clean R session and if the problem persists, contact the maintainer of the package for assistance. – Gavin Simpson Feb 29 '12 at 15:14
  • @lara I am a windows user as well and your code works for me. – Tyler Rinker Feb 29 '12 at 15:21