使用ggplot2在R图中包装文本

时间:2017-04-27 22:21:03

标签: r plot ggplot2

我需要在这个情节中包装文字。谁知道怎么做?

我已经尝试过使用check_overlap,但标签彼此非常分开。

    ex<-data.frame(x=1:10, y=c(1.732050808, 1.732050808, 1.732050808, 1.732050808, 0.866025404, 0.866025404, 0.866025404, 0.866025404, 0.866025404, 0.866025404
), z="Very very very very very very very very very very long text")

ggplot(ex,mapping = aes(x,y, label = (z)))+
  geom_text(size=2, check_overlap = F)+
  geom_point()

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用ggrepel包来避免重叠问题。您只需要将geom_text替换为geom_text_repel并删除参数check_overlap。例如:

library(ggrepel)
ggplot(ex,mapping = aes(x,y, label = (z)))+
  geom_text_repel(size=2)+
  geom_point()

输出: enter image description here