Suppose I would like to add text at the point (2, 2) so that it aligns with the line y = x.
If I use the angle
parameter for geom_richtext()
from the ggtext
package, I can see what happens if I set it to 45^\circ:
library(tidyverse)
library(ggtext)
tibble(x = 1:3, y = 1:3) %>%
ggplot(aes(x, y)) +
geom_line() +
geom_richtext(
x = 2, y = 2,
label = 'slope',
angle = 45
)
Created on 2021-03-24 by the reprex package (v0.3.0)
Not great, but even worse if I change the y-axis scale:
tibble(x = 1:3, y = 1:3) %>%
ggplot(aes(x, y)) +
geom_line() +
geom_richtext(
x = 2, y = 2,
label = 'slope',
angle = 45
) +
coord_cartesian(
ylim = c(0, 4)
)
So here's my question:
How can I represent the slope of a given line segment so that it's always aligned, no matter how the axis scale limits are chosen?