Using the identify function in R

25,259

Solution 1

# Here is an example

x = 1:10
y = x^2

name = letters[1:10]    
plot(x, y)

identify(x, y, labels = name, plot=TRUE)

# Now you have to click on the points and select finish at the end
# The output will be the labels you have corresponding to the dots.

Regarding saving it: I couldn't do it using

pdf() 
# plotting code
dev.off()

However in Rstudio it was posible to "copy-paste" it. If you need one plot only, i guess this would work.

Solution 2

You can use the return value of identify function to reproduce the labelling:

labels <- rep(letters, length.out=nrow(cars))
p <- identify(cars$speed, cars$dist, labels, plot=T)

#now we can reproduce labelling
plot(cars)
text(cars$speed[p], cars$dist[p], labels[p], pos=3)

To save the plot after using identify, you can use dev.copy:

labels <- rep(letters, length.out=nrow(cars))
identify(cars$speed, cars$dist, labels, plot=T)
#select your points here    

dev.copy(png, 'myplot.png', width=600, height=600)
dev.off()
Share:
25,259

Related videos on Youtube

user3358686
Author by

user3358686

Updated on February 12, 2020

Comments

  • user3358686
    user3358686 over 4 years

    In a scatterplot, I would like to use identify function to label the right top point.

    I did this:

    identify(x, y, labels=name, plot=TRUE)
    

    *I have a named vector.

    Then, while it is running, I point to the right point. Then after stopping it, it shows me the label of the point.

    Do I have to click the point that I want to label each time? Can I save it?

    • Rich Scriven
      Rich Scriven about 10 years
      Yes, you have to click on each point you want identified.