美文网首页R数据分析workshop
ggplot2以及画箭头的一切

ggplot2以及画箭头的一切

作者: 天地本无心 | 来源:发表于2021-01-05 13:59 被阅读0次

出来画图,难免碰到个画箭头的需求。 简单,如下的代码就能安排。

set.seed(123)
d = data.frame(x=sample(1:10,8, replace = FALSE),
               y=sample(1:5,8,replace = T),
               vx=runif(8,2,4),
               vy=runif(8,1,3))
ggplot() + 
  geom_segment(data=d, mapping = aes(x=x, y=y, xend=x+vx, yend=y+vy), arrow = arrow(length=unit(0.2, "cm"))) + 
  ylim(0,10) + 
  theme_bw()
ggplot画箭头

画箭头时一些调整和参数设置,可以参考如下代码,看看箭头的头和尾的圆或者钝怎么设置。

df2 <- expand.grid(lineend=c('round',"butt","square"), 
                   linejoin=c("round","mitre","bevel"),
                   stringsAsFactors = FALSE)

df2 <- data.frame(df2, y=1:9)

ggplot(df2, aes(x=1,y=y,xend=2, yend=y+1, label=paste(lineend, linejoin))) + 
  geom_segment(lineend = df2$lineend, linejoin = df2$linejoin, size=3, arrow = arrow(length=unit(0.3, "inches"))) + 
  geom_text(hjust="outside", nudge_x = -0.2) + 
  xlim(0.5,2) 
更多画箭头的细节

相关文章

网友评论

    本文标题:ggplot2以及画箭头的一切

    本文链接:https://www.haomeiwen.com/subject/sxoioktx.html