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
To add new points to an existing plot, use the points() function. The points function has many similar arguments to the plot() function, like x (for the x-coordinates), y (for the y-coordinates), and parameters like col (border color), cex (point size), and pch (symbol type).
To add a text to a plot in R, the text() and mtext() R functions can be used.
Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.
The “identify” tool in R allows you to quickly find outliers. You click on a point in the scatter plot to label it. You can place the label right by clicking slightly right of center, etc. The label is the row number in your dataset unless you specify it differenty as below.
You can easily create this by using the text()
function.
text(x,y,labels=names)
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.
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