如何在 ggplot 中将左侧的多个点连接到右侧的单个点?

How do I connect multiple points on left to single point on right in ggplot?

以下是我的数据示例和图表:

name <- c("PT02","PT02","PT02","PT02", "PT04","PT04","PT04","PT04", "PT05", "PT05","PT05", "PT05")
speed <- rep(c(145, 145, 145,150, 150, 150), 2)
position <- rep(c("Supine","Supine", "Up"), 4)
pct_change <- c( -32, -46, -72, -28, -60, -54, -24, -16, -36, -20, -15, -7)
df <- data.frame(name, speed, position, pct_change)

ggplot(df, aes(x=position, y = pct_change))+
  geom_point(aes(col = name), alpha = 0.5)+
  geom_line(aes(group=name))+
  facet_wrap(vars(speed))

使用上面的代码,直线垂直连接点。 我打算实现的是,对于每个名字,一个连接位置(仰卧和仰卧)的点。因此,如果我在 Supine 中有多个,而不是一条垂直线,则在 Up 中将每个连接成同一个名字。

当前图表:

我希望它看起来像的示例:(没有垂直线)

我试过 geom_line 和 geom_path,并按名称分组,但我似乎无法让它工作。我在某处读到 group=1 有帮助,但也无法正常工作。

我创建了 df 的旋转副本并将其传递给 geom_segment

library(tidyverse)

df %>% 
  pivot_wider(names_from = position, values_from = pct_change, values_fn = list) %>% 
  unnest(Supine) %>% 
  unnest(Up) %>%
  ggplot(aes(x = "Supine", y = Supine)) +
  geom_point(data = df, aes(position, pct_change, col = name), alpha = 0.5) +
  geom_segment(aes(xend = "Up", yend = Up)) +
  facet_wrap(vars(speed))