4

Thanks for the suggested duplicate, this is however not only about the labels, but is also about adjusting the points themselves so they do not overlap.

have a quick look at the plot below...

I need the coloured points, and their corresponding labels, to never overlap. They should be clustered together and all visible, perhaps with some indication that they are spaced and not 100% accurate, perhaps some sort of call out? Open to suggestions on that.

I've tried adding position = 'jitter' to both geom_point and geom_text, but that doesn't seem to be working (assume it is only for small overlaps?) Ideas?

# TEST DATA
srvc_data <- data.frame(
  Key = 1:20,
  X = sample(40:80, 20, replace = T),
  Y = sample(30:65, 20, replace = T)
)
srvc_data$Z <- with(srvc_data,abs(X-Y))


t1<-theme(                              
  plot.background = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  panel.border = element_blank(), 
  panel.background = element_blank(),
  axis.line = element_line(size=.4)
)

main_plot <- ggplot(srvc_data, aes(x = X, y = Y),xlim=c(0,100), ylim=c(0,100)) +
  t1 +
  theme_bw() +
  labs(x="X", y="Y") +
  scale_x_continuous(limits = c(0, 100)) +
  scale_y_continuous(limits = c(0, 100)) +
  geom_abline(intercept = 0, slope = 1, colour="blue", size=34, alpha=.1)+
  geom_abline(intercept = 0, slope = 1, colour="black", size=.2, alpha=.5,linetype="dashed")+
  geom_point(size = 7, aes(color = Z), alpha=.7) + 
  scale_color_gradient("Gap %\n",low="green", high="red")+
  coord_fixed()+
  geom_text(aes(label=Key,size=6),show_guide = FALSE)
main_plot

Produces this plot (of course with your random data it will vary)

Example plot

Thanks in advance.

Alex
  • 971
  • 4
  • 15
  • 28
  • 2
    would geom_jitter() help? – MLavoie Dec 07 '15 at 23:01
  • 1
    Have you investigated what you can do with the `directlabels`-package? – IRTFM Dec 07 '15 at 23:14
  • 1
    As an alternative to `directlabels`, you could also have a look at `wordcloud`: e.g. sth like `srvc_data <- cbind(srvc_data, with(srvc_data, wordcloud::wordlayout(X, Y, rep("\u2b24", length(X)), cex=50))[, c("x", "y")])` and then `ggplot(srvc_data_, aes(x = x, y = y)...` to use the newly calculated values (lower case `x` and `y`). – lukeA Dec 07 '15 at 23:20
  • 1
    Possible duplicate of [Labelling points with ggplot2 and directlabels](http://stackoverflow.com/questions/33258075/labelling-points-with-ggplot2-and-directlabels) – alexwhitworth Dec 08 '15 at 00:54
  • 1
    Jitter had a slight effect when I tried it but the default setting doesn't change the plot enough to notice if you're not looking closely. But jittering the points will add noise to your data--is it ok for your purposes? Is reducing the diameter of the colored data points and offsetting the labels an option for you? – tsurudak Dec 10 '15 at 04:42
  • @tsurudak The addition of some noise is not a problem, these are really for illustrative purposes, and not for scientific consumption per se (for lay persons and executives). Ideally the points would always contain the numbers though rather than offsetting them. – Alex Dec 11 '15 at 05:51

1 Answers1

3

Here's your plot with ggrepel geom_text_repel:

library(ggrepel)
# TEST DATA
set.seed(42)
srvc_data <- data.frame(
  Key = 1:20,
  X = sample(40:80, 20, replace = T),
  Y = sample(30:65, 20, replace = T)
)
srvc_data$Z <- with(srvc_data,abs(X-Y))


t1<-theme(                              
  plot.background = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  panel.border = element_blank(), 
  panel.background = element_blank(),
  axis.line = element_line(size=.4)
)

ggplot(srvc_data, aes(x = X, y = Y),xlim=c(0,100), ylim=c(0,100)) +
  t1 +
  theme_bw() +
  labs(x="X", y="Y") +
  scale_x_continuous(limits = c(0, 100)) +
  scale_y_continuous(limits = c(0, 100)) +
  geom_abline(intercept = 0, slope = 1, colour="blue", size=34, alpha=.1)+
  geom_abline(intercept = 0, slope = 1, colour="black", size=.2, alpha=.5,linetype="dashed")+
  geom_point(size = 7, aes(color = Z), alpha=.7) + 
  scale_color_gradient("Gap %\n",low="green", high="red")+
  coord_fixed()+
  geom_text_repel(aes(label=Key,size=6),show_guide = FALSE)

enter image description here

Kamil Slowikowski
  • 4,184
  • 3
  • 31
  • 39