2

I'd like to use the package directlabels to label my plot. However, I'd like the label to be the ID of each point. Is there really no way to select which factor to label or did I miss that?

library(ggplot2)
library(directlabels)
df <- structure(
    list(id = 1:10,
         group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
         .Label = c("A", "B"),
         class = "factor"),
         value1 = c(4, 1, 6, 2, 5, 7, 3, 2, 5, 8),
         value2 = c(6, 2, 6, 2, 8, 9, 7, 5, 2, 6)
         ),
.Names = c("id", "group", "value1", "value2"),
row.names = c(NA, -10L),
class = "data.frame")

p1 <- ggplot(df, aes(x=value1, y=value2)) + geom_point(aes(colour=group))
direct.label(p1)

enter image description here

rbm
  • 3,243
  • 2
  • 17
  • 28
erc
  • 10,113
  • 11
  • 57
  • 88
  • 2
    Why not just use `geom_text`? – Jaap Sep 28 '15 at 09:25
  • 2
    `direct.label` is used as an alternative of the legends (i.e. it is used to show the groups in your graph) not as a way to add labels on each point. For point labels use `geom_text` as Jaap mentions in his comment. – LyzandeR Sep 28 '15 at 09:28
  • @Jaap because my actual plot consists of around 200 points and with `geom_text` a lot of the labels are overlapped – erc Sep 28 '15 at 09:32

2 Answers2

5

Inspecting the code of direct.label.ggplot() shows that geom_dl() is called in the end. This function expects an aesthetic mapping and a positioning method. The positioning method used by default is the return value of default.picker("ggplot"), which uses call stack examination and in your case is equivalent to calling defaultpf.ggplot("point",,,). The following works for me:

p1 <- ggplot(df, aes(x=value1, y=value2)) +
  geom_point(aes(colour=group)) +
  geom_dl(aes(label = id), method = defaultpf.ggplot("point",,,))
p1

Plot

(Note that you don't need to call direct.label() anymore.)

The documentation of the directlabels package is indeed a bit scarce.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • fantastic, thank you! I just have one more problem.. In my actual plot there a LOT of points very close together and some of the labels are now far away from the points, is there a way to limit the distance between point and label? – erc Sep 30 '15 at 11:29
  • Could you perhaps post an example in a separate question? – krlmlr Sep 30 '15 at 11:43
  • From my experience with `direct labels`, in general it works fairly well to avoid overlapping labels; except, of course, in the case where you have many many points that are close together. There are a few different spacing options you can select within `direct labels`, but I haven't found a solution to the issue you are posing (i.e., the labels always seem to be far away from the points, if they aren't overlapping). It's the issue of using a generalized function for a specific problem. – rhozzy Oct 04 '15 at 22:58
1

You have to write a custom position method, like so:

label_all <- list( dl.trans(x = x + 0.5, y = y + 0.5), # shift every point up and right
                   gapply.fun(d) ) # include all points
direct.label(p1, method = label_all)

enter image description here

For another example, see the documentation, under "Specifying the positioning method as a list".

ROLO
  • 4,183
  • 25
  • 41
  • 1
    From my understanding, OP wants to take advantage of the positioning method provided by `directlabels`. Your answer simply uses a default positioning method, which in my opinion also could have been achieved via `geom_text` as suggested in the comments. – krlmlr Sep 30 '15 at 09:55
  • Seems you're right, and a nice answer you gave indeed. I might make use of that some day as well. – ROLO Sep 30 '15 at 11:16