Display Values in R Plot


How can I render the value of points in a plot in the plot itself?

Thank you.

9/19/2009 7:11:37 PM

Accepted Answer

b0 = 2.5; b1 = 2
n = 100
x = rnorm(n, 20, 15)
y = b0 + b1*x + rnorm(n, 0, 15)
plot(x, y)
plot(x, y, type='n')
text(x, y, round(y, 2), cex=0.45)
text(x, y, round(y, 2), cex=0.8)
text(x, y, paste(round(x, 2), round(y, 2), sep=", "), cex=0.8) # for (x, y), but this gets cluttered. 

alt text

Use cex for character size (see help for text). And use plot(x, y, type='n') to set up your window correctly without actually plotting anything.

8/6/2016 4:18:45 PM

Use text():

plot(1:10, 1:10) 
text(5, 5, "Foo")

and see help(text) for options on placing the text. The function is vectorised so you can also do something akin to

 text(1:10, 1:10, LETTERS[1:10])

if you have vectors of text and positions.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow