我想使用 ggplot2 绘制两条线之间的角度,其含义类似于下图中的粗红线。对此有简单的解决方案吗?
制作没有红线的图的数据和代码:
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()
最佳答案
看看 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)
您必须稍微调整一下才能以更健壮、更自动的方式使用它,例如:
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)
关于r - ggplot2 绘制两条线之间的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277320/