ggplot2 geom_line()应指向指定的值

Sar*_*est 5 r ggplot2

我写了以下代码:

library(ggplot2)

data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L, 
                    1L, 5L), y = c(1L, 7L, 5L, 6L, 3L, 4L, 6L, 2L, 5L, 6L, 5L, 2L
            ), year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                            2L, 2L), .Label = c("2010", "2011"), class = "factor"), matching = structure(c(1L, 
                            2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("person1", 
                            "person2", "person3", "person4", "person5", "person6"), class = "factor")), .Names = c("x", 
            "y", "year", "matching"), row.names = c(NA, -12L), class = "data.frame")

data$year <- factor(data$year)

colors <- c("#4cb5ee", "#a0d099", "red")

p <- ggplot(data, aes(x=x, y=y)) +
    geom_point(aes(colour=year), shape=16, size=6) +
    geom_line(aes(group=matching), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1) +
    xlab("x") + ylab("y") +
    scale_colour_manual("year", values=colors) +
    scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
    scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)
Run Code Online (Sandbox Code Playgroud)

它给出了以下输出: 情节

但是我想要geom_line()做的是:总是指向年份= 2011年的点.我无法弄清楚为什么线的箭头有时指向年份= 2010年的点,有时指向年份= 2011年的点.

我发现的是箭头有几个参数:

arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open") 
Run Code Online (Sandbox Code Playgroud)

所以我可以说ends="first".但我无法概括ends总是first或永远last.

我试图在我的data.frame中添加一个列,如果箭头应该是第一个或最后一个结果,它有信息,但它没有给我我想要的输出.

每一个帮助都非常感谢:-)

提前致谢!

rcs*_*rcs 11

geom_path 应该做的伎俩:

p <- ggplot(data, aes(x=x, y=y)) +
    geom_point(aes(colour=year), shape=16, size=6) +
    geom_path(aes(group=matching), 
                  arrow=arrow(length=unit(0.15,"cm")),
                  colour="black", size=1) +
    xlab("x") + ylab("y") +
    scale_colour_manual("year", values=colors) +
    scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
    scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)
Run Code Online (Sandbox Code Playgroud)

geom_path图