2016-11-11 63 views
3

Figure如何抖动/删除重叠的geom_text标签

在图中,是有可能抖动状态缩写标签一下,让他们不重叠?如果我使用check_overlap = TRUE,那么它会删除一些重叠的观察结果,而我不希望这样。我也不想geom_label_repel,因为它的标签,贴出来和跨越45度线我包括移动(我不希望发生的事情)

这里是我的代码,以供参考相关部分:

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) + 
    geom_abline(intercept = 0) + 
    geom_text(fontface = "bold") 
+0

无[再现的代码](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)不可能直接帮助你。但是,您应该仔细查看'ggrepel'以查找对标签移动的控制。 –

回答

4

您试过position=position_jitter()?您可以根据自己的选择调整widthheight

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) + 
    geom_abline(intercept = 0) + 
    geom_text(fontface = "bold",position=position_jitter(width=1,height=1)) 

EDIT 一个例子来操纵一个特定标签仅

+geom_text(fontface = "bold", 
position=position_jitter(width=ifelse(df$abbrev=='KS',1,0), 
     height=ifelse(df$abbrev=='KS',1,0))) 

或者多个标签

df$jit<-with(df, ifelse(abbrev == "KS" | abbrev == "LA", 1, 2)) 

+geom_text(fontface = "bold", 
    position=position_jitter(width=df$jit,height=df$jit)) 
+0

谢谢!你碰巧知道是否有办法抖动特定的文本,而不是所有的文本?例如,在上面的图像示例中,是否有一种方法可以使用“KS”和“LA”文本抖动观察值,但是没有其他观察值? –

+0

编辑可能有一些帮助...? –