Anthony
Anthony

Reputation: 427

Alignment issues with geom_label function in ggplot2 R

When trying to align the top left corner of the geom_label() box to the (0,10) coordinate with ggplot2, the text is not centered anymore (see picture). Is it possible to have a top left aligned box with centered text inside?

Picture

library(ggplot2)
data=data.frame(x=0:10,y=0:10)
ggplot(data,aes(x,y)) + geom_point() + geom_label(x=0,y=10,hjust=0,vjust=1,label="TEXT",size=8)

Upvotes: 3

Views: 649

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24790

Here's an approach with geom_richtext from the ggtext package:

#install.packages("ggtext")
library(ggtext)
ggplot(data,aes(x,y)) +
  geom_point() +
  geom_richtext(x=0,y=10,hjust=0,vjust=1,label="TEXT",size=8,
                label.padding = unit(c(0.25,0.25,0,0.25), "lines"))

enter image description here

Upvotes: 1

Related Questions