是否可以以gggenes中的geom_gene_arrow这样的格式绘制箭头?

问题描述 投票:0回答:1

我希望在绘图中加入箭头形状来表示基因,就像使用

gggenes
geom_gene_arrow
一样,也就是说,粗箭头最好可以与条形重叠。这可能吗?

https://cran.r-project.org/web/packages/gggenes/vignettes/introduction-to-gggenes.html

r ggplot2 plotly gggenes
1个回答
0
投票

解决这个问题的一个明显方法是将箭头自己制作为多边形。以下函数将处理几何图形:

make_arrows <- function(start, stop, yvals, groups, head_length = 500,
                        body_width = 0.1, head_width = 0.15) {
  
  dfs <- Map(function(start, stop, yval, group, arrow_num) {
    if(stop - start > head_length) {
    x <- c(start, start, stop - head_length, stop - head_length, stop,
           stop - head_length, stop - head_length, start, start)
    y <- c(yval, yval + body_width/2, yval + body_width/2, yval + head_width/2,
           yval, yval - head_width/2, yval - body_width/2, yval- body_width/2,
           yval)
    } else {
      x <- c(start, start, stop, start, start)
      y <- c(yval, yval + head_width/2, yval, yval - head_width/2, yval)
    }
    data.frame(x = x, y = y, arrow_num = arrow_num, group = group)
  }, start = start, stop = stop, yval = yvals, 
  group = groups, arrow_num = seq_along(start))
  do.call(rbind, dfs)
}

然后,假设你有这样的数据:

df <- data.frame(start = c(15200, 17000, 18000, 18400, 19000, 22300, 22400,
                           23500, 25000),
                 stop = c(17000, 18000, 18400, 19000, 22300, 22400, 
                          23500, 25000, 27000),
                 Gene = LETTERS[1:9])

然后你可以按如下方式绘制它:

library(ggplot2)

p <- ggplot(make_arrows(df$start, df$stop, 1, df$Gene), aes(x, y, fill = group)) +
  geom_polygon(color = "black") +
  scale_y_continuous(NULL, limits = c(0.7, 1.3), 
                     breaks = 1, labels = "Genome1") +
  labs(x = NULL, y = NULL) +
  scale_fill_manual(values = c("#8cd2c6", "#fdfcb3", "#bdb9da", "#fa7f72",
                               "#7fb0d2", "#fbb361", "#feea6e", "#bb7ebd",
                               "#cceac3"), guide = "none") +
  facet_wrap(.~"Genome1") +
  theme_gray(base_size = 20) +
  theme(legend.position = "none")

p

你会得到一个很好的结果:

plotly::ggplotly(p)

© www.soinside.com 2019 - 2024. All rights reserved.