如何防止出现错误的箭头?
x<-seq(-2,2,by=0.1)
df<-rbind(data.frame(x,y=sin(x),type="sin"),data.frame(x,y=cos(x),type="cos"))
ggplot(df,aes(x,y))+
geom_point()+
facet_grid(. ~ type)+
geom_segment(aes(xend=c(tail(x, n=-1), NA), yend=c(tail(y, n=-1), NA)),arrow=arrow(length=unit(0.2,"cm")))
R 3.3.2
ggplot2_2.2.1
答案 0 :(得分:1)
这是因为您只在NA
和xend
中插入一个yend
。这有效:
xx=seq(-2, 2, 0.1)
df<-rbind(data.frame(x=xx,y=sin(xx),type="sin"),data.frame(x=xx,y=cos(xx),type="cos"))
ggplot(df,aes(x,y))+
geom_point()+
facet_grid(. ~ type)+
geom_segment(aes(xend=rep(c(tail(xx, n=-1), NA), 2), yend=c(tail(y, n=-1), NA)),arrow=arrow(length=unit(0.2,"cm")))