I would like to create a ggplot graph with labels instead of points, but they overlie each other, so that you can not read them. Is there a nice way to automatically shift them just enough that they do not overwrite each other?
df = data.frame(x = c(1,4,5,6,6,7,8,8,9,1), y = c(1,1,2,5,5,5,3,5,6,4),
label = rep(c("long_label","very_long_label"),5))
ggplot(data=df) + geom_text(data=df,aes(x=x, y=y, label = label))
Thank you
Answer
ggrepel
(released to CRAN yesterday - 9 Jan 2016) seems tailor-made for these situations. But note: ggrepel
requires ggplot2
v2.0.0
df = data.frame(x = c(1,4,5,6,6,7,8,8,9,1), y = c(1,1,2,5,5,5,3,5,6,4),
label = rep(c("long_label","very_long_label"),5))
library(ggplot2)
library(ggrepel)
# Without ggrepel
ggplot(data = df) +
geom_text(aes(x = x, y = y, label = label))
# With ggrepel
ggplot(data = df) +
geom_text_repel(aes(x = x, y = y, label = label),
box.padding = unit(0.1, "lines"), force = 2,
segment.color = NA)
No comments:
Post a Comment