2015-12-07 45 views
3

感謝您提供的重複建議,但這不僅僅是關於標籤,而且還關於調整這些點本身,使它們不重疊。防止geom_points和它們相應的標籤重疊

有一個快速瀏覽一下下面的情節......

我需要的顏色點,及其相應的標籤,永遠不會重疊。他們應該聚集在一起,都是可見的,也許有一些跡象表明他們是間隔的,並不是100%準確的,也許某種呼喚?接受有關這方面的建議。

我試過在geom_point和geom_text中添加position ='jitter',但似乎沒有工作(假設它僅適用於小重疊?) 想法?

# 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

在此先感謝。

+2

將geom_jitter()幫助? – MLavoie

+1

你有沒有調查過你可以用'directlabels'包做什麼? –

+1

作爲「直接標籤」的替代方案,您還可以查看'wordcloud':例如(srvc_data,srvc_data,wordcloud :: wordlayout(X,Y,rep(「\ u2b24」,length(X)),cex = 50))[,c(「x」,「使用新計算的值(小寫'x'和'y') – lukeA

回答

1

這裏是你的陰謀與ggrepelgeom_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