2017-02-15 52 views
1

如何防止出现错误的箭头?当使用facet_grid时,geom_segment会产生错误的箭头

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"))) 

enter image description here

[R 3.3.2

ggplot2_2.2.1

回答

1

这是因为你只插入一个NA到您的xendyend。此作品:

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"))) 
相关问题