R 如何通过gpplot中的不同组将点与线连接?

R 如何通过gpplot中的不同组将点与线连接?,r,ggplot2,R,Ggplot2,我想画一张图,图中的点与线相连。我遇到的问题是,我在同一列下的点与不同的值关联,因此当我添加geom_线时,它作为一条线穿过所有组。我怎样才能分开这条线,使它只连接具有相同变量的点 下面是一些示例代码 temps = data.frame(Temperature= c(15,25,35), Growth.Phase = c("exponential", "stationary", "death"),

我想画一张图,图中的点与线相连。我遇到的问题是,我在同一列下的点与不同的值关联,因此当我添加
geom_线
时,它作为一条线穿过所有组。我怎样才能分开这条线,使它只连接具有相同变量的点

下面是一些示例代码

temps = data.frame(Temperature= c(15,25,35), 
                                    Growth.Phase = c("exponential", "stationary", "death"),
                                    Carbohydrates = sample(c(3:10), 9, replace = T))

temps$Shape = if_else(temps$Growth.Phase == "exponential", 21,
                      if_else(temps$Growth.Phase == "stationary", 22, 23))
我想要3条线,每条线用相同的符号连接点,但当我使用“geom_line”时,例如

ggplot(data = temps, aes(x = Temperature, y = "Proportions")) +
  geom_point(aes(y = Carbohydrates),colour = "darkred", 
             fill = "darkred", shape = temps$Shape, size = 3) +
  geom_line(aes(y = Carbohydrates))
我得到这个图像


有人知道如何解决这个问题吗?

您可以将
形状
放在第一个美学中,然后在
geom\u行中使用它作为分组变量

ggplot(data = temps, aes(x = Temperature, y = "Proportions", shape = factor(Shape))) +
  geom_point(aes(y = Carbohydrates),colour = "darkred", 
             fill = "darkred", size = 3) +
  geom_line(aes(y = Carbohydrates))


如果直线不垂直时,点的顺序不正确,可以使用
temps%>%dplyr::arrange(carbohydes)

直线应该是垂直的吗?好的,这只是示例数据中的结果,但最终,它看起来不会是垂直的。