r - ggplot2 绘制两条线之间的角度

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

最佳答案

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

enter image description here

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

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)

enter image description here

关于r - ggplot2 绘制两条线之间的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277320/

相关文章:

r - ggplot2 不显示 geom_abline 中特征的图例

r - 使用 arules 包在 R 中进行离散化

R 真的很慢矩阵/data.frame 索引选择

r - 用于准确预测的数据集中的最小行数

r - 在 knitr 中打印动态大小的绘图列表

r - ggplot、ggplotly、scale_y_连续、ylim 和百分比

r - 使用 dplyr 为每组应用 ggplot 函数并为每组设置标题

r - 使用 ggplot 和 plotly 绘制累积密度图

r - 使用 Ryacas 在 R 中进行符号计算 - 结果变成字符

r - ggplot2 在列之间创建不均匀的间距