R: how to optimize the position of labeling in plot

2.2k views Asked by At

Hi I guess that I have quite a rudimentary question here. I have a plot like thisenter image description here but as you could easily notice, some of the label could not be displayed (some are overlapped with the symbols, some are just out of the figure frame) I noticed that there are some way to adjust the position of labels

text(tsne_out$Y[,1], tsne_out$Y[,2], labels=samplegrouptry, pos=1)

for example, I could specify the the value of "pos" (from 1 to 4). I guess they are good enough in most cases .But I wonder whether there are some better ways to do that. Any suggestion, thanks!

Following the suggestion from

vas_u Through change the axis ranges as well as "pos", I could get better plot: enter image description here

1

There are 1 answers

1
vas_u On

One way around the problem would be to enlarge the axes of the plot.

Your example approximately reproduced with dummy data:

x <- rnorm(16, mean = 0)
y <- rnorm(16, mean = 1)

# Initial scatterplot with text labels out of plot area:

plot(x, y, pch = 16)
text(x, y, labels = paste("Name", 1:16), pos = 1) # Some labels outside plot area

# Second plot with the X and Y axes gently expanded:

plot(x, y, pch = 16, 
    xlim = 1.1*range(x), 
    ylim = 1.1*range(y)) 
text(x, y, labels = paste("Name", 1:16), pos = 1) # Labels now fit inside!

I hope this helps.