0

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")

clipped text

GeeGeePlottR
  • 25
  • 1
  • 4

3 Answers3

1

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")

Created on 2022-07-02 by the reprex package (v2.0.1)

Carl
  • 4,232
  • 2
  • 12
  • 24
  • Thank you @Carl, that's probably what I'll go for. I'm surprised that ggplot2 doesn't handle this well by default but I guess there's a logic to it given the elegance of the package as a whole. – GeeGeePlottR Jul 04 '22 at 08:21
1

Interesting. Looks like this issue is related to what is chosen as the base line to align the text labels. This could be seen clearly when switching to geom_label where we see that for the clipped label the base line chosen for the alignment is not the end of the "p". Hence the "p"s get clipped off:

ggplot(df) +
  geom_point(mapping = aes(x = x, y = y)) +
  annotate("label", x = mean(df$x), y = -Inf, label = "Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "red", label.padding = unit(0, "lines")) +
  annotate("label", x = mean(df$x), y = Inf, label = "Not Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "blue", label.padding = unit(0, "lines"))

enter image description here

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"))

stefan
  • 90,330
  • 6
  • 25
  • 51
0

If you don't want it to be clipped on the same position, you can use coord_cartesian(clip = "off"):

df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
                 ,y=c(0,1))
library(ggplot2)
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") +
  coord_cartesian(clip = 'off')

Created on 2022-07-02 by the reprex package (v2.0.1)

Quinten
  • 35,235
  • 5
  • 20
  • 53