I have a plot with labelled rectangles. To ensure text stays inside the rectangle I'm using the ggfittext package. Example below:
library(ggplot2)
library(ggfittext)
df <- data.frame(
xmin = c(1000, 4000), xmax = c(3000, 5000),
ymin = c(1, 3), ymax = c(2, 5),
label = "My Label"
)
p <- ggplot(df, aes(xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax)) +
geom_rect(fill = "grey60")
p + geom_fit_text(aes(label = label),
size = 40)

As one might notice, the label in the second rectangle on the right would fit better if it were rotated, and the label wouldn't need to be shrunk as much. I can easily do that manually:
p + geom_fit_text(aes(label = label),
size = 40, angle = c(0, 90))

Created on 2021-09-29 by the reprex package (v2.0.1)
However, I'd like to automatically detect if text placement is better horizontally or vertically. Is there an option to do this in ggfittext or alternative packages? I'm searching for an answer where the visual dimensions are more important than the data dimensions: these rectangles in the example are much wider than they are tall in data-space, but not in visual-space.
