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

3
我需要为我的 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
@IRTFM 抱歉,我编辑了问题。这是字体族。 - Laura
2个回答

4
您可以使用ggtext ::geom_textbox()来显示注释,并关闭实际的框部分。请注意,您需要提前知道x轴上的限制来计算盒子在归一化父坐标(npc单位)中的正确宽度。在本例中*,如果您想在1-100的刻度上从0到40,您需要计算(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包 (v2.0.0) 在2021-12-11创建

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


不错!如果我想缩小宽度,文本会自动调整吗? - Laura
是的,请尝试调整您的图形窗口大小,例如它应该适合可用空间。 - teunbrand
感谢您的时间,@teunbrand! - Laura
1
我不确定我完全理解你所说的“span(?)”。你是指固定字符数的文本换行吗? - teunbrand
是的,我也在寻找那个选项,但是我在文档中没有找到它。不过,我看到 width 接受一个 unit() 对象,如果您事先知道 x 轴,可以反向推导出应该是什么。我在 GH 上没有找到相关问题,因此也许值得提出一个功能请求? - teunbrand
显示剩余2条评论

3
使用 `ggfittext::geom_fit_text` 及其 `xmin` 和 `xmax` 美学元素。使用 `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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接