2

I was looking for a way to avoid overlapping of labbels in scatterplots. I found directlabels which is working fine but does not allow me to use expressions as labels nor to select which labels I want to be displayed and which I don't.

Here is an example :

p1 use geom_text() to plot some (the ones I choose) labels with expression parsing allowing things like greek letters and superscript but labels can overlap.

p2 avoid overlapping with directlabels() but I can't control which labels are ploted nor use expression parsing.

How can I have the advantages of both methods ?

require(ggplot2)
require(directlabels)

par <- c("Ei", "Gb", "Gf", "Gl", "w1", "w2", "w3", "w4",
     "w5", "Alpha", "Beta", "Amin", "dl", "df")
df <- data.frame(x=runif(14), y=runif(14), Parameters=par)

# labels with expressions
labels <- c("Ei" = 'E[i]', "Gb" = 'G[b]', "Gf" = 'G[f]', "Gl" = 'G[l]', 
             "w1" = 'omega[1]', "w2" = 'omega[2]', "w3" = 'omega[3]', "w4" = 'omega[4]', 
             "w5" = 'omega[5]', "Alpha" = 'alpha', "Beta" = 'beta', "Amin" = 'A[min]', 
             "dl" = 'd[l]', "df" = 'd[f]')

# and missing values
labels[which(sqrt(df$x^2 + df$y^2) < .5)] <- NA


p1 <- ggplot(data=df, aes(y=y, x=x, colour=Parameters)) + 
  geom_point() + scale_colour_discrete() +
  coord_cartesian(xlim=c(-2,3), ylim=c(-2,3)) + # causes overlapping
  geom_text(aes(label = labels), hjust=-0.3, vjust=1, 
            show_guide=FALSE, parse=TRUE) 

p1 # selected labels appear with correct expression


p2 <- direct.label(p1, method='smart.grid')
p2 # all labels appear (not wanted) without expression parsing
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • Maybe this can help: you can choose which one is being displayed with the 'cex' argument setting zero values for the ones you want hidden : `cex <- rep(1, length(labels))` `cex[which(sqrt(df$x^2 + df$y^2) < .8)] <- 0` `p2 <- direct.label(p1, list(cex=cex))` – Yollanda Beetroot Oct 21 '14 at 07:25

0 Answers0