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

时间:2015-12-07 22:53:50

标签: r ggplot2 scatter-plot

感谢建议的重复,但这不仅仅是关于标签,还涉及调整点本身,使它们不重叠。

快速浏览下面的情节......

我需要彩色点及其相应的标签,以免重叠。它们应该聚集在一起并且全部可见,或许有一些迹象表明它们是间隔的并且不是100%准确,也许是某种呼唤?对此提出建议。

我已经尝试将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

生成此图(当然,随机数据会有所不同)

Example plot

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是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