Home > OS >  How to set a width of annotation text in ggplot
How to set a width of annotation text in ggplot

Time:12-11

I need to set an specific width for my text annotation in ggplot. For example, I need that my 2 annotations bellow to go from x = 0 to x = 40 and the second annotation from x = 50 to x = 90. In other words I need to make these annotation fit the spaces between 0 and 40 and 50 and 90.

Also make the text be well align.

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')

Only using ggplot package is possible to set this?

Is there any package that help me to do this?

CodePudding user response:

You can use ggtext::geom_textbox() to display annotations, and turn off the actual box part. Note that you need to know your x-limits in advance to calculate the correct width of the box in normalised parental coordinates (npc units). In this case* if you want from 0 to 40 on a scale of 1-100, you need to calculate (40-0)/((100 - 1) * 1.1). The 1.1 is the default scale expansion factor (5% on both sides). Added some vertical lines to show that text is inside those bounds.

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

Created on 2021-12-11 by the reprex package (v2.0.0)

* should actually be (40 - 0) / ((100 - 0) * 1.1) because I didn't take into account that the x = 0 of the annotation affected the scale.

  • Related