簡體   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