0

I need to produce automatically a plot where the dots are connected to their labels (which are outside) through a line. Is there a way to do this in R?. Thanks in advance.

See here an example: https://www.dropbox.com/s/4tepdp5j13ot2dd/Example.png?dl=0

  • 1
    Are you sure that's how you want it done? I cannot think of any automated way to do so, and, well, it's kind of ugly. Why not use colors, shapes, or print the label name next to the dot? All of these options can be automatically generated and, in my opinion, would look better. – Andrew Taylor Apr 21 '15 at 16:55
  • Thank you for the answer, the problem is that I have to many points in the plot (more than 5000) and I want to label 10 or so. I tried those options but none of them were fine to me. I will try the two answers below to see which one is the better for my purpose. – Andrés Arturo Lanzós Camaioni Apr 22 '15 at 11:00

2 Answers2

2
set.seed(1)
x <- rnorm(10)
lab <- sample(1:10, 5)
par(mar = c(5,4,2,5))
plot(x)
p <- par('usr')
# l <- legend(p[2], p[4], legend = round(x[lab], 2), xpd = NA, bty = 'n')
l <- legend(p[2], p[4], legend = sprintf('Label%s', 1:5), xpd = NA, bty = 'n')
segments(lab, x[lab], l$text$x, l$text$y, xpd = NA, col = seq_along(lab))

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • I don't think this is the plot @Andrés is looking for. From the dropbox, at least how I understood it, it was a scatterplot of x and y, with labels indicating some z. I could be wrong though. – Andrew Taylor Apr 21 '15 at 17:15
  • @AndrewTaylor I dont understand your comment. One could change the labels literally to anything if one so desired – rawr Apr 21 '15 at 17:17
  • Sorry, mountain out of molehill, I suppose. I was just stating that given solution did not have a 1:1 match with what I was interpreting out of the linked plot. Which can be either an issue of my misinterpretation of the given problem, or my unnecessary want for an emphasis of exactness over minimal-yet-conducive to answering the problem type code. – Andrew Taylor Apr 21 '15 at 17:32
  • @AndrewTaylor oh I see what you mean. I was just generating some random labels since there weren't any labels given – rawr Apr 21 '15 at 17:38
  • Thank you very much, this is exactly the plot that I want to generate automatically. – Andrés Arturo Lanzós Camaioni Apr 22 '15 at 11:01
0

Based on @rawr's solution:

set.seed(1)
x <- rnorm(10)
y <- rnorm(10)
z <- letters[1:10]
df<-data.frame(x,y,z)

lab <- df$z
plot(x,y)
p <- par('usr')
l <- legend(p[2], p[4], legend = lab, xpd = NA, bty = 'n')
segments(df$x, df$y, l$text$x, l$text$y, xpd = NA)

enter image description here

But now let's say that each label has more than 1 x,y pair:

set.seed(1)
x <- rnorm(20)
y <- rnorm(20)
z <- letters[10:1]
df<-data.frame(x,y,z)[order]

lab <- levels(df$z)
plot(x,y)
p <- par('usr')
l <- legend(p[2], p[4], legend =lab, xpd = NA, bty = 'n')
lab_df<-data.frame(x=l$text$x, y=l$text$y, z=lab)
do_seg<-function(i){
  segments(df[df$z==i,]$x, df[df$z==i,]$y, lab_df[lab_df$z==i,]$x, lab_df[lab_df$z==i,]$y, xpd = NA)
}
lapply(lab,do_seg)

Gives us:

enter image description here

Andrew Taylor
  • 3,438
  • 1
  • 26
  • 47