繁体   English   中英

将整个文本放入 plot 区域

[英]Fit entire text into a plot area

我想用文本创建一个 plot 但似乎 ggplot 在识别 x 和 y 限制时忽略了文本(它只使用中心点)。 因此,如果文本太长,则无法完全显示。

df <- tibble(x = 1:5, y = 1:5)
ggplot(df, aes(x, y, label = paste(rep("long text", 3), collapse = " "))) +
  geom_point() + geom_text(nudge_y = 0.2)

在此处输入图像描述

在这种情况下,可以添加层expand_limits(x = c(0.4, 5.6))但这不适用于任何其他情况。 我想要一些适用于 x 和 y 的任何值以及任何文本长度的东西。

您可以为此使用ggrepel

library(ggrepel)

ggplot(df, aes(x, y, label = paste(rep("long text", 3), collapse = " "))) +
  geom_point() + ggrepel::geom_text_repel(nudge_y = 0.2, segment.alpha = 0)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM