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?
# 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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With