首页 > 解决方案 > 如何在ggplot2中的线段之间绘制角弧?

问题描述

是否可以使用 ggplot2(或 plotnine 或其他图形包语法)在两条直线段之间绘制角弧,如下所示?

(忽略圆圈等)

在此处输入图像描述

我知道这可以通过 Geogebra 等图形程序来完成。但我有兴趣在 Jupyter 中以编程方式绘制角度标记(和标签)。

(对了,这个“角弧”有没有一个词?我不知道怎么称呼,就用了“角弧”。)

标签: ggplot2jupyter-notebook

解决方案


对于 R,有ggforce一个扩展 ggplot2 并定义了一个geom_arc()非常接近的包。下面的例子:

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


推荐阅读