ggplot2 绘制两条线之间的角度

Ste*_*ted 2 r ggplot2

我想使用 ggplot2 绘制两条线之间的角度,这意味着类似于下图中的粗体红线。有没有简单的解决方案?

plot_with_red_line

制作没有红线的图的数据和代码:

library(tidyverse)

df <- tibble(
  line = c("A", "A", "B", "B"),
  x = c(1, 5, 1, 3),
  y = c(1, 3, 1, 5))

ggplot(
  df, aes(x, y, group = line))+
  geom_path()
Run Code Online (Sandbox Code Playgroud)

Arc*_*tte 5

看看geom_curve,例如:

ggplot(  df, aes(x, y, group = line))+
  geom_path() +
  geom_curve(aes(x = 1.5, y = 2, xend = 2, yend = 1.5), curvature = -0.5, color = "red", size = 3)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

您必须稍微调整一下才能以更强大、更自动的方式使用它,例如:

red_curve <- df %>%
  group_by(line) %>%
  summarise( avg_x = mean(x),
             avg_y = mean(y))

ggplot(  df, aes(x, y, group = line))+
  geom_path() +
  geom_curve( data = red_curve, aes(x = avg_x[1], y = avg_y[1], xend = avg_x[2], yend = avg_y[2]), curvature = 0.5, color = "red", size = 3)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明