Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent geom_points and their corresponding labels from overlapping

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.

like image 561
Alex Avatar asked Oct 27 '25 10:10

Alex


1 Answers

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

like image 131
Kamil Slowikowski Avatar answered Oct 29 '25 23:10

Kamil Slowikowski