gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:05:48 25 4
gpt4 key购买 nike

我需要在 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 有可能吗?有没有其他软件包可以帮助我做到这一点?

最佳答案

您可以使用 ggtext::geom_textbox() 来显示注解,并关闭实际的框部分。请注意,您需要提前知道您的 x 限制,以便以标准化 parent 坐标(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 创建于 2021-12-11 (v2.0.0)

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

关于r - 在ggplot中设置注释文本的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70311270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com