Search code examples
rggplot2annotate

R: ggplot2 Change the margins of box around text in annotate(geom="label"...)


Is there a way to control the margin size of the box around the text ?

x <- data.frame(x = c(5, 10), y = c(0.5, 1))

ggplot(data=x, aes(x, y)) +
  geom_bar(stat = 'identity', fill=c("red4","cornflowerblue"))+
  annotate(geom= "label", x=5, y=.6, label="Just\ntext\nhere\nwith\ndifferent\nmargins", size=5)

enter image description here


Solution

  • There is the parameter label.padding. See documentation.

    ggplot(data=x, aes(x, y)) + 
      geom_bar(stat = 'identity', fill=c("red4","cornflowerblue")) + 
      annotate(geom= "label", x=5, y=.6, 
               label="Just\ntext\nhere\nwith\ndifferent\nmargins", size=5,
               label.padding=unit(4, "lines"))    # <------------