简体   繁体   English

将整个文本放入 plot 区域

[英]Fit entire text into a plot area

I want to create a plot with text but it seems that ggplot ignores text when identifying x and y limits (it uses only centerpoint).我想用文本创建一个 plot 但似乎 ggplot 在识别 x 和 y 限制时忽略了文本(它只使用中心点)。 So if the text too long it is not fully visible.因此,如果文本太长,则无法完全显示。

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)

在此处输入图像描述

In this case one can add layer expand_limits(x = c(0.4, 5.6)) but that doesn't work for any other case.在这种情况下,可以添加层expand_limits(x = c(0.4, 5.6))但这不适用于任何其他情况。 I would like to have something that works for any values of x and y and any length of text.我想要一些适用于 x 和 y 的任何值以及任何文本长度的东西。

You can use ggrepel for this:您可以为此使用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