我需要在 ggplot 中為我的文本注釋設定特定的寬度。例如,我希望第一個注釋從x = 0to 開始x = 40,第二個注釋從x = 50to 開始x = 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 實作嗎?有沒有其他軟體包可以幫助我做到這一點?
uj5u.com熱心網友回復:
您可以使用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

由
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381237.html
