I'm trying to avoid the bottom annotation being clipped. It's the descender on the "p" that gets chopped off. I've used the "inward" option on vjust.
df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
,y=c(0,1))
df
ggplot(df)
geom_point(mapping=aes(x=x,y=y))
annotate("text",x=mean(df$x),y=-Inf,label="Clipped",hjust=0.5,vjust="inward",size=12,colour="red")
annotate("text",x=mean(df$x),y=Inf,label="Not Clipped",hjust=0.5,vjust="inward",size=12,colour="blue")
CodePudding user response:
A possible approach would be to use the min
and max
y values:
library(tidyverse)
df <- data.frame(
x = c(as.Date("2020-01-01"), as.Date("2022-01-01")),
y = c(0, 1)
)
ggplot(df)
geom_point(aes(x, y))
annotate("text", x = mean(df$x), y = min(df$y), label = "Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "red")
annotate("text", x = mean(df$x), y = max(df$y), label = "Not Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "blue")
One possible fix would be to switch to ggtext::GeomRichtext
:
library(ggplot2)
library(ggtext)
ggplot(df)
geom_point(mapping = aes(x = x, y = y))
annotate(ggtext::GeomRichtext, x = mean(df$x), y = -Inf, label = "Clipped",
hjust = 0.5, vjust = "inward", size = 12, colour = "red",
label.size = 0, fill = NA, label.padding = unit(0, "lines"))
annotate(ggtext::GeomRichtext, x = mean(df$x), y = Inf, label = "Not Clipped",
hjust = 0.5, vjust = "inward", size = 12, colour = "blue",
label.size = 0, fill = NA, label.padding = unit(0, "lines"))