Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set a width of annotation text in ggplot

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any package that help me to do this?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading