0

can you help me how to have data labels in my R chart

date = c(1.45,0.89,0.74,3.28)
plot(date,type = "l")

enter image description here

I need the percentages to be displayed

how can i achieve this

r2evans
  • 141,215
  • 6
  • 77
  • 149
redondo
  • 107
  • 1
  • 1
  • 6
  • 2
    This tagged ggplot but you don't seem to be using ggplot. Do you have a specific requirement? Also, this might help: https://stackoverflow.com/questions/7611169/intelligent-point-label-placement-in-r or this https://stackoverflow.com/questions/15624656/label-points-in-geom-point – MrFlick Aug 30 '21 at 21:23

1 Answers1

5

Use text.

First, though, I'll expand the axis limits to make room.

plot(date, type = "l", xlim = c(0.5, 4.5), ylim = range(date) + c(-0.5, 0.5))
text(seq_along(date), date+0.2, paste(date, "%"))

simple plot

Arbitrary components:

  • I expanded 0.5 below and 0.5 above the range of date, may need to play with this if your data changes
  • I used date + 0.2 to shift the numbers slightly above the points;
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Depending on your real data, you may prefer to replace `paste(date,"%")` with `sprintf("%0.02f%%",date)` in order to limit the number of decimal places. – r2evans Aug 30 '21 at 22:22