如何抖动文本,以避免在ggplot2 scatterplot重叠?

我想在ggplot2中创build一个干净的文本标签散点图。 目标是视觉上呈现与大约25个项目相关的不断增加的价值。 我已经在使用“position_jitter”,但我想知道我能做得更好。

这里是一些模拟数据:

title <- rep("A Really Rather Long Text Label", 25) value <- runif(25, 1,10) spacing <- seq(1:25) df <- data.frame(title, value, spacing, stringsAsFactors = FALSE) 

这里是生成图的代码:

 library(ggplot2) myplot <- ggplot(data=df, aes(x=spacing, y=value, label = title)) + geom_text(aes(colour = value), size = 2.5, fontface = "bold", vjust = 0, position = position_jitter(width=5, height=0)) + theme_bw() + scale_x_continuous(limits = c(-5, 30))+ scale_colour_gradient(low = "#6BAED6", high = "#08306B") + theme(axis.title.x = element_blank(), axis.ticks = element_blank(), axis.text.x = element_blank(), legend.position = "none") myplot 

在合理大小的图表中,所有这些文字都有足够的空间 – 只要文本可以自由移动,只要它需要水平移动即可! 我不想垂直抖动,因为重点是显示与每个文本标签关联的y值。

每次运行ggplot命令时,graphics都会略有不同 – 有时抖动结果比其他时间更好。 但我还没有find一个系统地防止标签重叠的方法。

我很感激任何有关如何清理文本的水平位置的build议,而不必手动移动单个项目。 我也很高兴听到关于如何改善这种types的视觉表示的其他技巧。

看看FField

 install.packages("FField", type = "source") install.packages("ggplot2") install.packages("gridExtra") library(FField) FFieldPtRepDemo() 

(从这个posthttps://stats.stackexchange.com/a/62856 )