在ggplot中设置注释文本的宽度

Posted

技术标签:

【中文标题】在ggplot中设置注释文本的宽度【英文标题】:Set the width of annotation text in ggplot 【发布时间】:2022-01-15 13:04:45 【问题描述】:

我需要在 ggplot 中为我的文本注释设置一个特定的宽度。例如,我希望第一个注释从x = 0x = 40,第二个注释从x = 50x = 90。换句话说,我需要使这些注释适合 0 到 40 之间以及 50 到 90 之间的空间。

我还希望文本对齐。

library(ggplot2)
librray(tibble)

df <- tibble(x = 1:100, y = 1:100)

ggplot(df, aes(x = x, y = y))+ geom_blank() +

  annotate(geom = 'text', x = 0, y = 50, hjust = 0, vjust = 0,
           label = 'Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ',
 size = 3,
color  ='black')+
  annotate(geom = 'text', x = 50, y = 50, hjust = 0, vjust = 0,
           label = 'Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ',
size = 3,
color  ='black')

ggplot2 有可能吗?有没有其他软件包可以帮助我做到这一点?

【问题讨论】:

该代码出现“未使用的参数”错误。 @IRTFM 抱歉。我编辑了这个问题。这是字体系列 【参考方案1】:

您可以使用ggtext::geom_textbox() 显示注释,并关闭实际框部分。请注意,您需要提前知道您的 x 限制,以便以标准化父母坐标(npc 单位)计算框的正确宽度。在这种情况下*如果您想要从 0 到 40 的范围为 1-100,则需要计算 (40-0)/((100 - 1) * 1.1)。 1.1 是默认的比例扩展因子(两边均为 5%)。添加了一些垂直线以显示文本在这些边界内。

library(ggplot2)
library(ggtext)
#> Warning: package 'ggtext' was built under R version 4.1.1

df <- data.frame(x = 1:100, y = 1:100)

width <- (40-0)/((100 - 1) * 1.1)

ggplot(df, aes(x = x, y = y))+ geom_blank() +
  
  annotate(geom = 'text_box', x = 0, y = 70, hjust = 0, vjust = 1,
           label = 'Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ',
           family = 'Yanone Kaffeesatz', size = 3, width = unit(width, "npc"),
           color  ='black', fill = NA, box.colour = NA, box.padding = margin())+
  annotate(geom = 'text_box', x = 50, y = 70, hjust = 0, vjust = 1,
           label = 'Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ',
           family = 'Yanone Kaffeesatz', size = 3, width = unit(width, "npc"),
           color  ='black', fill = NA, box.colour = NA, box.padding = margin()) +
  geom_vline(xintercept = c(0, 40, 50, 90))
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

由reprex package (v2.0.0) 于 2021 年 12 月 11 日创建

* 实际上应该是 (40 - 0) / ((100 - 0) * 1.1) 因为我没有考虑到注释的x = 0 会影响比例。

【讨论】:

不错!而且,如果我想减少宽度,文本会自行调整。对吗? 是的,例如尝试调整绘图窗口的大小,它应该适合可用空间。 感谢您抽出宝贵时间@teunbrand! @teunbrand,不错!向ggplot添加具有一定跨度的文本应该更容易吗?我错了吗? 我不确定我是否完全理解您对 span (?) 的意思是指使用固定数量的字符换行的文本吗?【参考方案2】:

使用ggfittext::geom_fit_text 及其xminxmax 美学。使用place 调整文本并使用padding.x 删除(默认)填充

library(ggplot2)
library(ggfittext)

ggplot() +
  geom_fit_text(aes(xmin = c(0, 50), xmax = c(40, 90),
                    y = 50,
                    label = c('Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ',
                              'Shortly after his arrival at Real Madrid in July 2018, ESPN journalist Dermot Corrigan described Vinícius
as a "zippy left winger or second striker". El Mundo described him "A player who is forever tormented,
teetering on the edge, on that invisible line between genius and ridicule. Judgment always hangs over
him, a winger unable to make his legs and feet move in unison, but he never gives up and that is a great
quality. A versatile player, although he is usually deployed on the left flank, he is capable of playing
anywhere along the front line,  and has also been used on the right or in the centre. ')),
                reflow = TRUE, place = "left", padding.x = grid::unit(0, "mm")) +
  xlim(0, 100)

【讨论】:

以上是关于在ggplot中设置注释文本的宽度的主要内容,如果未能解决你的问题,请参考以下文章

如何在 QTreeWidget 中设置小列宽?

如何在 iOS 中设置视图的最大宽度?

在 SwiftUI 中设置 TextField 的宽度

R语言ggplot2可视化:通过在element_text函数中设置ifelse判断条件自定义标签文本的显示格式:例如粗体斜体等

在工具栏中设置灵活宽度的文本字段

在 QTreeWidget 中设置编辑器宽度以填充单元格