我正在尝试绘制点与直线相连的图形。我遇到的问题是,同一列下的点与不同的值相关联,因此当我添加点时,geom_line
它作为1条线穿过所有组。如何分隔线,使其仅连接具有相同变量的点?
这是一些示例代码
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))
我得到这张图片
有谁知道如何解决这一问题?
您可以先放入shape
美学元素,然后将其用作geom_line
分组变量
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(Carbohydrates)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句