How to draw an angle arc between line segments in ggplot2?

Viewed 231

Is it possible to use ggplot2 (or plotnine or other grammar of graphics packages) to draw an angle arc between two straight line segments as shown below?

(ignore the circle etc.)

enter image description here

I know that this can be done with graphics programs such as Geogebra. But I am interested in drawing the angle mark (and label) programmatically in Jupyter.

(By the way, is there a word for this "angle arc"? I don't know how to call it, and just used "angle arc".)

1 Answers

For R, there is the ggforce package that extends ggplot2 and defines a geom_arc() that comes pretty close. Example below:

library(ggplot2)
library(ggforce)

start <- c(x = 0, y = 0)

dat <- data.frame(
  x = start[c("x", "x")],
  y = start[c("y", "y")],
  xend = c(1, 4),
  yend = c(5, 1)
)

angles <- with(dat, atan2(xend - x, yend - y))

ggplot(dat) +
  geom_segment(aes(x, y, xend = xend, yend = yend)) +
  geom_arc(aes(x0 = start["x"], y0 = start["y"], r = 1, 
               start = angles[1], end = angles[2])) +
  coord_equal()

Related