简体   繁体   English

防止geom_points及其相应的标签重叠

[英]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? 它们应该聚集在一起并且全部可见,或许有一些迹象表明它们是间隔的并且不是100%准确,也许是某种呼唤? 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? 我已经尝试将position ='jitter'添加到geom_point和geom_text,但这似乎不起作用(假设它只适用于小重叠?)想法?

# 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) 产生这个图(当然随着你的随机数据会有所不同)

示例图

Thanks in advance. 提前致谢。

Here's your plot with ggrepel geom_text_repel : 这是你用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)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM