2

I am trying to make a scatter plot which uses numbers for each point on the graph, rather than black dots. I can do this for numbers 9 and under, but anything 10 and above is represented by 1 on the resulting plot. The numbers represent individual subjects, and I have 13 of them, with a number of data points from each individual.

I am using the plot function for this as I have worked out how to do all the other things I need with that so I am reluctant to change to another plot type/command.

Sample code:

plot(dif ~ mean, pch=as.character(Subject.Number), data = Cov5.1)

But pch=as.character only works for single digit numbers. I have seen solutions on here that talk about using text or labels but for whatever I cannot get either of those to work.

Here is one previous question, but the answer doesn't work for me: R- plot numbers instead of points

How can I create a scatter plot which uses numbers instead of dots?

user213544
  • 2,046
  • 3
  • 22
  • 52
Zlata 80
  • 31
  • 3
  • See https://www.r-graph-gallery.com/275-add-text-labels-with-ggplot2/ – IceCreamToucan Oct 30 '18 at 13:05
  • Thank you, but I am trying to use the numbers as the actual data points on the plot rather than as labels. Labels will just make it look too cluttered. In addition I think I will have to start from scratch with all the commands if I use ggplot rather than plot. I have various other lines, labels and titles to put on and I only know how to code for these in plot. – Zlata 80 Oct 30 '18 at 13:10

1 Answers1

5

You can do this by displaying the numbers as text, rather than "points". Just make an empty plot and then add the text.

set.seed(1066)
x = runif(13)
y = runif(13)

plot(x,y,type="n")
text(x,y, labels=1:13, cex=0.8)

Plotting with numbers

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Ah ok! So is it the type="n" that tells it to make the plot blank? That's fabulous! Thank you! – Zlata 80 Oct 30 '18 at 14:09
  • Yes, type="n" means set up the plotting area for this data, but do not plot the points. – G5W Oct 30 '18 at 14:13
  • 2
    I have made this work! For how my data is presented I had to change it to: text(x=Cov5.1$mean, y=Cov5.1$dif,labels=Cov5.1$Subject.Number) otherwise it just seemed to be randomly assigning the numbers 1-13. Thank you so much! I was getting nowhere and wasting so much time trawling thru stuff online. I am very grateful. – Zlata 80 Oct 30 '18 at 14:27