Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the identify function in R

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?

like image 950
user3358686 Avatar asked Jan 11 '23 18:01

user3358686


2 Answers

# 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.

like image 145
marbel Avatar answered Jan 13 '23 07:01

marbel


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()
like image 31
gkcn Avatar answered Jan 13 '23 07:01

gkcn