查看原文
其他

ggarchery 添加个性化箭头

JunJunLab 老俊俊的生信笔记 2023-06-15


匆匆

1引言

今天分享一个 ggarchery R 包,用来添加箭头,达到不一样的效果。

github 地址:

https://github.com/cran/ggarchery

2安装

install.packages('ggarchery')

3使用

构造数据:

library(tidyverse)
library(ggarchery)

tbl <- tibble(x = c(0.10.2), xend = c(0.10.8), y = c(0.10.5), yend = c(0.70.9))

传统绘图:

ggplot(tbl) +
  geom_segment(aes(x = x, xend = xend, y = y, yend = yend), arrow = arrow()) +
  xlim(c(0,1)) +
  ylim(c(0,1))

geom_arrowsegment 函数添加箭头:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend)) +
  xlim(c(0,1)) +
  ylim(c(0,1))

调整箭头类型:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrows = arrow(type = 'closed')) +
  xlim(c(0,1)) +
  ylim(c(0,1))

控制箭头位置:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrow_positions = 0.5) +
  xlim(c(0,1)) +
  ylim(c(0,1))

调整箭头类型:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrow_positions = 0.5, arrows = arrow(type = 'closed')) +
  xlim(c(0,1)) +
  ylim(c(0,1))

映射颜色:

tbl <- tbl %>% mutate(col = c("A""B"))

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend, col = col), arrow_positions = 0.5)  +
  xlim(c(0,1)) +
  ylim(c(0,1))

添加箭头数量:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrow_positions = c(0.250.75))  +
  xlim(c(0,1)) +
  ylim(c(0,1))

设置 1 则为末端:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrow_positions = c(0.251))  +
  xlim(c(0,1)) +
  ylim(c(0,1))

调整不同角度,类型:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend), arrow_positions = c(0.251), arrows = list(arrow(angle = 10), arrow(type = 'closed')))  +
  xlim(c(0,1)) +
  ylim(c(0,1))

添加箭头填充颜色:

ggplot(tbl) +
  geom_arrowsegment(aes(x = x, xend = xend, y = y, yend = yend),
                    arrow_positions = c(0.251),
                    arrow_fills = c("indianred3""dodgerblue3"),
                    arrows = arrow(type = "closed"))  +
  xlim(c(0,1)) +
  ylim(c(0,1))

作为注释:

ggplot(mtcars) +
  geom_point(aes(x = disp, y=hp)) +
  annotate(geom = "arrowsegment",
           x = 170,
           y=200,
           xend = 145,
           yend = 175,
           arrow_positions = 0.6,
           arrows = arrow(type = "closed", length = unit(0.1"inches")))

4position_attractsegment 修整箭头

pt.tbl <- tibble(x = c(0.250.50.75), y = c(0.250.50.75), labels = c("A""B""C"))

ggplot(pt.tbl) +
  geom_point(aes(x,y, fill = labels), size =6, shape = 21) +
  geom_text(aes(x,y, label = labels)) +
  xlim(c(01)) +
  ylim(c(01)) +
  scale_fill_discrete(guide = "none")

添加箭头:

sg.tbl <- tibble(x = c(0.250.5), y = c(0.250.5), xend = c(0.50.75), yend = c(0.50.75))

ggplot(pt.tbl) +
  geom_point(aes(x,y, fill = labels), size =6, shape = 21) +
  geom_text(aes(x,y, label = labels)) +
  geom_segment(data = sg.tbl, aes(x = x, xend = xend, y = y, yend = yend), arrow = arrow()) +
  xlim(c(01)) +
  ylim(c(01)) +
  scale_fill_discrete(guide = "none")

修整:

ggplot(pt.tbl) +
  geom_point(aes(x,y, fill = labels), size =6, shape = 21) +
  geom_text(aes(x,y, label = labels)) +
  geom_segment(data = sg.tbl,
               aes(x = x, xend = xend, y = y, yend = yend),
               arrow = arrow(),
               position = position_attractsegment(start_shave = 0.1, end_shave = 0.1)) +
  xlim(c(01)) +
  ylim(c(01)) +
  scale_fill_discrete(guide = "none")

靠近点,减小间距:

ggplot(pt.tbl)+
  geom_segment(data = sg.tbl, aes(x = x, xend = xend, y = y, yend = yend), arrow = arrow(),
               position = position_attractsegment(start_shave = 0, end_shave = 0.05, type_shave = "distance")) +
  geom_point(aes(x,y, fill = labels), size =6, shape = 21) +
  geom_text(aes(x,y, label = labels))  +
  xlim(c(01)) +
  ylim(c(01)) +
  scale_fill_discrete(guide = "none") +
  coord_fixed()

建议 xy 范围一致 或者使用 coord_fixed() 函数。

组合使用:

ggplot(pt.tbl)+
  geom_arrowsegment(data = sg.tbl, aes(x = x, xend = xend, y = y, yend = yend),
                    arrow_positions = 0.6,
                    arrows = arrow(length = unit(0.1"inches")),
                    position = position_attractsegment(start_shave = 0, end_shave = 0.05, type_shave = "distance")) +
  geom_point(aes(x,y, fill = labels), size =6, shape = 21) +
  geom_text(aes(x,y, label = labels))  +
  xlim(c(01)) +
  ylim(c(01)) +
  scale_fill_discrete(guide = "none") +
  coord_fixed()

5结尾

不足:

Current these replace only geom_segment() and work only for linear coordinate systems. I would like to extend to geom_curve() but the intricacies of grid::curveGrob() make that much more complicated. I am unsure if allowing these for the geom_line() and geom_path() parameterisations would be especially useful, but I'm happy to be told they would be.





  老俊俊生信交流群 (微信交流群需收取20元入群费用(防止骗子和便于管理))



老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!




  





scRNAtoolVis 0.0.3 版本更新

customize 你的 GSEA 图

GseaVis 优雅的可视化 GSEA 富集结果

GSEA 图是如何画出来的? (源码解析)

clusterProfiler 的可视化大全

clusterProfiler 做 GSEA 富集分析

深度神经网络学习对单细胞数据进行清洗去噪

ggpie 解决你的所有饼图绘制

Bigwig 可视化用 tackPlotR 试试看?

gggsea 个性化绘制 GSEA 图

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存