R - ggplot2“堆积”点之间的跟踪线

时间:2016-10-15 18:47:13

标签: r ggplot2

我无法确定如何在两个点之间追踪线条(最终是箭头)。所以我的基本情节看起来像这样:

 dt %>% 
  ggplot(aes(x = V2, y = V3, colour = V1) ) + geom_point(size = 3) + geom_point() +
  scale_colour_manual(values = c('gray44', 'black')) + 
  theme_minimal(base_size = 16) + geom_text(aes(x = V2, y = V3+1.05, label = V1)) 

enter image description here

所以,我试图创建“群组”以便追踪一条线但它不起作用, 我想要的是,彼此之上的点数相互关联:

dt = dt %>% group_by(V1) %>% mutate(grp = 1:n())

dt %>% 
  ggplot(aes(x = V2, y = V3, colour = V1) ) + geom_point(size = 3) + geom_point() +
  scale_colour_manual(values = c('gray44', 'black')) + 
  theme_minimal(base_size = 16) + geom_text(aes(x = V2, y = V3+1.05, label = V1)) +
  geom_path(data = dt, aes(V2, V3, group = grp))

最终,我会非常有兴趣追踪从19851990的箭头,但我无法理解它

我尝试添加类似的内容:

geom_segment(aes(x = V3[V1 == 1985], y = V3[V1 == 1985], xend = V3[V1 == 2000], yend = V3[V1 == 2000]), arrow = arrow(length = unit(0.5, "cm"))) 

但它不起作用。

有什么想法吗?

enter image description here

dt = structure(list(V1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1985", 
"1990"), class = "factor"), V2 = structure(c(1L, 2L, 3L, 1L, 
2L, 3L), .Label = c("A", "B", "C"), class = "factor"), V3 = c(60, 
40, 60, 80, 20, 40)), .Names = c("V1", "V2", "V3"), row.names = c(NA, 
-6L), class = "data.frame")

0 个答案:

没有答案