Ole
Ole

Reputation: 107

How to change font size in geom_text() outside plot area?

I'd like to annotate a plot and I'd like the note to be outside the plot area. I found this solution and it works for adding a note outside the plot area, but I cannot figure out how to change the label's appearance (most importantly, for my purpose, the font size).

Here is a minimal example from the aforementioned solution:

library (ggplot2)
library(grid)

df=data.frame(y=c("dog1","dog2","dog3"),x=c(12,10,14),n=c(5,15,20))
p <- ggplot(df, aes(x,y)) + geom_point()

# Add the annotation
p <- p + geom_text(aes(label = "Hello World!", x = 0, y = 0), vjust = 2, hjust = 1)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

Ideally, the note would be in the bottom left corner.

Upvotes: 5

Views: 8351

Answers (1)

Kurt_Brummert
Kurt_Brummert

Reputation: 161

library (ggplot2)
library(grid)

df=data.frame(y=c("dog1","dog2","dog3"),x=c(12,10,14),n=c(5,15,20))
p <- ggplot(df, aes(x,y)) + geom_point()

# Add the annotation
p <- p + geom_text(size=8, colour="red", aes(label = "Hello World!", x = 0, y = 0), vjust = 2.5, hjust = 1)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

Upvotes: 3

Related Questions