我试图将多个点连接到一个点,按单个变量分组。我的问题类似于这个OP,但它们有多个点,而不是一个。
这是一个数据框,用于说明我正在使用的数据类型:
A <- data.frame(
Stage = c("Juvenile", "Juvenile", "Yearling",
"Juvenile", "Juvenile", "Yearling",
"Juvenile", "Juvenile", "Yearling",
"Juvenile", "Juvenile", "Yearling"),
Individual = c ("A", "A", "A",
"B", "B", "B",
"C", "C", "C",
"D", "D", "D"),
Score = c( 1.4, 1.2, NA,
0.4, 0.6, 0.5,
-0.3, -0.5, -0.4,
-1.4, -1.2, NA))
我能够得到的最接近的图形是使用此代码(为简单起见,仅显示准系统代码):
ggplot(A, aes(x = Stage, y = Score, color =Individual, group= Individual)) +
geom_point() +
geom_line(aes(group=Individual)+
geom_smooth(aes(x = Stage),
method=lm, se=F, fullrange=TRUE, color="black")
相反,我需要更像这样的东西(手绘):
我如何能:
Juvenile
列中的点与列中的单个点连接起来Yearling
(当那里有一个点时)?Juvenile
列内的点相互连接起来Individual
?这是使用单独的准备表进行连接的方法:
A_connections <- A %>%
filter(Stage == "Juvenile") %>%
left_join(A %>% filter(Stage == "Yearling") %>% select(Individual, Y_Score = Score))
ggplot(A, aes(x = Stage, y = Score, color = Individual, group= Individual)) +
geom_point() +
geom_segment(data = A_connections, aes(xend = "Yearling", yend = Y_Score)) +
geom_smooth(method=lm, se=F, fullrange=TRUE)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句