我必须添加一个额外的列z2
to get ggplot2
将每一行视为一个单独的段。否则,仅在每组线段的末尾绘制箭头。代码解释得最好:
library(grid)
library(ggplot2)
x <- c(100, 200, 300, 200, 500, 320, 300, 50)
y <- c(100, 250, 600, 700, 60, 120, 200, 360)
t <- rep(seq(2009,2012),2)
z <- rep(c("A","B"),each=4)
z2 <- LETTERS[1:length(z)]
d <- as.data.frame(cbind(z,z2,t,x,y))
d <- d[order(d$z, d$t),]
ggplot(data = d, aes(x = x, y = y, colour = z2, label=t)) +
geom_path(aes(group = z), arrow = arrow(ends = "last")) +
geom_point() +
geom_text()
...以及结果
现在你只需要稍微调整一下色阶,或者使用不同的美学z2
.