Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to label a pretty simple scatterplot in R. This is what I use:

plot(SI, TI)
text(SI, TI, Name, pos=4, cex=0.7)

The result is mediocre, as you can see (click to enlarge):

enter image description here

I tried to compensate for this using the textxy function, but it's not better. Making the image itself larger doesn't work for the dense clusters.

Is there any function or easy way to compensate for this and let R plot labels that don't overlap?

Here is a small subset of the data I have:

share|improve this question

closed as off-topic by gung, Sven Hohenstein, Christoph Hanck, amoeba, Stephan Kolassa Jan 18 at 17:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – gung, Sven Hohenstein, Christoph Hanck, amoeba, Stephan Kolassa
If this question can be reworded to fit the rules in the help center, please edit the question.

I posted similar question here, have a look on the answers there.. – Curious Sep 30 '11 at 20:22
Thanks. Good to know! @Tomas – slhck Sep 30 '11 at 20:33
I found a solution! The identify() let's you manually decide where to place the label! It's not ideal, but from the proposed solutions this works best for me. – Curious Oct 28 '11 at 15:35
up vote 14 down vote accepted

Check out the new package ggrepel. ggrepel provides geoms for ggplot2 to repel overlapping text labels. It works both for geom_text and geom_label.

enter image description here

Figure is taken from this blog post.

share|improve this answer
This is perfect, thank you! – slhck Jan 19 at 14:23

The directlabels package does that. From its web page:

This package is an attempt to make direct labeling a reality in everyday statistical practice by making available a body of useful functions that make direct labeling of common plots easy to do with high-level plotting systems such as lattice and ggplot2.

It might not always be possible for dense plots, though.

Here is a short example:

a <- c(rnorm(10,-3,2),rnorm(10,3,2))
b <- c(rnorm(10,-3,2),rnorm(10,3,2))
dfr <- data.frame(a,b)
dfr$t <- c(paste("A",1:10,sep=""),paste("B",1:10,sep=""))
direct.label(xyplot(b~a,dfr,groups=t, col="black"))

I did manage get rid of the point colouring with col="black", but the not labels.

share|improve this answer
I'm having trouble getting it to work. Could you maybe provide a simple working example? – slhck Sep 26 '11 at 16:31
In your case, something like direct.label(xyplot(SI~TI,data=yourDataFrame,group=Name)) should get a similar result. – Laurent Sep 26 '11 at 17:13
Perfect. Here's what I ended up with using your last simple example. The color labels and points are actually very nice, since you know where the labels belong. – slhck Sep 26 '11 at 17:22
I had to use library(lattice) to get xyplot to work. – David J. Harris Sep 4 '13 at 22:26

I'd suggest you take a look at the wordcloud package. I know this package focuses not exactly on the points but on the labels themselves, and also the style seems to be rather fixed. But still, the results I got from using it were pretty stunning. Also note that the package version in question was released about the time you asked the question, so it's still very new.

textplot() output

share|improve this answer

I ran into a similar problem with several of the plots I have been working with and wrote a basic package that uses force field simulation to adjust object locations. The advantage over some of the above-cited solutions is the dynamic adjustment for relative object proximity in 2D. While much improvement is possible, including heuristics and integration with ggplot, etc. it seems to get the task accomplished. The following illustrates the functionality:

install.packages("FField", type = "source")

For now there is no heuristics for a variety of areas and point distributions as the solution met my needs and I wanted to get something helpful to folks out quickly but I'll add these in the medium term. At this time I recommend scaling charts to 100x100 and back and slightly tweaking the default attraction and repulsion parameters as warranted.

share|improve this answer

A couple of additional tools to look at in R:

These won't do everything for you, but one of them may be part of a solution.

share|improve this answer

In the event that you simply cannot get the labels to work correctly as produced by R, keep in mind you can always save the graphs in a vector format (like .pdf) and pull them into an editing program like InkScape or Adobe Illustrator.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.