36

I would like to create a clean version of a scatterplot of text labels in ggplot2. The goal is to represent visually the increasing values associated with about 25 items. I am already using "position_jitter," but I wonder if I can do better.

Here is some mock data:

title <- rep("A Really Rather Long Text Label", 25)
value <- runif(25, 1,10)
spacing <- seq(1:25)
df <- data.frame(title, value, spacing, stringsAsFactors = FALSE)

And here is the code that generates the graph:

library(ggplot2)
myplot <- ggplot(data=df, aes(x=spacing, y=value, label = title)) +
geom_text(aes(colour = value),
    size = 2.5, fontface = "bold",
    vjust = 0,
    position = position_jitter(width=5, height=0)) +
theme_bw() +
scale_x_continuous(limits = c(-5, 30))+
scale_colour_gradient(low = "#6BAED6", high = "#08306B") +
theme(axis.title.x = element_blank(),
      axis.ticks = element_blank(),
      axis.text.x = element_blank(),
      legend.position = "none")
myplot

There is plenty of space for all of this text in a graph of reasonable size -- so long as the text is free to shift as far as it needs to horizontally! I don't want to jitter vertically, because the point is to show the y value associated with each text label.

The graph comes out slightly differently every time you run the ggplot command -- and sometimes the jittering results are better than other times. But I haven't found a way to systematically prevent the labels from overlapping.

I'd be grateful for any suggestions on how to clean up the horizontal positioning of the text without having to move individual items manually. I'd also be glad to hear other tips on how to improve this type of visual representation.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
user1257313
  • 1,057
  • 4
  • 11
  • 10
  • 5
    Almost a duplicate; at the very least you may find [this](http://stackoverflow.com/q/7611169/324364) question about as informative as any answer you'll get here. – joran Jun 25 '12 at 21:24
  • Thanks for the link - I would not have found it through the terms I was searching, and it is useful to know when there is no easy automated solution. Under that previous question, you make a good argument for a manual solution. In that spirit, here's another fast (though limited) way to tweak the PDF plot: open in Adobe Acrobat, choose the "Edit Document Text" tool, and add spaces at the start of an overlapping label. This route only makes it possible to shift selected text to the right, but it might be enough for someone who is not looking for a publication-quality plot. – user1257313 Jun 26 '12 at 02:25
  • 1
    Well, I also pointed you to that post because it has a pretty comprehensive list of functions/packages that attempt to do it for you. – joran Jun 26 '12 at 05:09
  • @user1257313, [this SO answer](http://stackoverflow.com/a/10526607/1305688) might also be helpful. – Eric Fail Jan 19 '13 at 01:48
  • 1
    It looks like this CV post may also answer the same question, does it resolve your issue? [How do I avoid overlapping labels in an R plot?](http://stats.stackexchange.com/questions/16057/how-do-i-avoid-overlapping-labels-in-an-r-plot) – Thell May 01 '14 at 16:05
  • @joran: what is the best we can get in plain ggplot2, without other packages? – smci May 03 '14 at 06:39
  • This question pops up way too often, every time we refer to the same few hacks. Hadley, where are you? Please implement an intelligent and automatic solution for this problem/geom_text() – Vlo Jul 15 '14 at 18:33
  • 4
    Nobody is stopping you @Vlo to find a solution for this problem. Get the code from github, update it and file a pull request. I'm sure that Hadley will merge any nice solution into ggplot2. – Thierry Jul 24 '14 at 07:33
  • 1
    This is one of those problems that sound easy and are remarkably hard to actually solve. There is no inherent solution other than trying a number of packages, `directlabel` being perhaps the most promising one. If the data is not too tight and the axes are both continuous, you might be able to use ggplot's `geom_text` and specify an offset. – Chris vCB Oct 24 '14 at 23:01

1 Answers1

6

Have a look at the FField package

install.packages("FField", type = "source")
install.packages("ggplot2")
install.packages("gridExtra")
library(FField)
FFieldPtRepDemo()

(from this post https://stats.stackexchange.com/a/62856)

Community
  • 1
  • 1
Jonas
  • 1,639
  • 1
  • 18
  • 29