如何使用 R 中的 ggplot2 将基因与线条连接

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

我有一个基因数据框,我想绘制它并用线连接它

head(df)
      sample       contig  start    end         qseqid  gene       cluster position
1 Genome-300 Genome-300_1 199743 201911 IMEHDJCA_00189 geneE cluster_00365 200827.0
2 Genome-300 Genome-300_1 201914 203275 IMEHDJCA_00190 geneD cluster_01313 202594.5
3 Genome-300 Genome-300_1 203272 205377 IMEHDJCA_00191 geneB cluster_00403 204324.5
4 Genome-300 Genome-300_1 206268 206663 IMEHDJCA_00193 geneC cluster_05858 206465.5
5 Genome-300 Genome-300_1 206686 222306 IMEHDJCA_00194 geneA cluster_00001 214496.0
6 Genome-306 Genome-306_1 287568 289736 DIBHEKPI_00259 geneE cluster_00365 288652.0

因此使用 gggenes geom_gene_arrow 进行绘图

第一个情节:

ggplot(df, aes(xmin = start, xmax = end, x = position, y = sample, fill = gene, label = gene)) +
    geom_gene_arrow(arrow_body_height = unit(7, "mm"), 
                    arrowhead_height = unit(7, "mm"), 
                    arrowhead_width = unit(5.7, "mm")) +
    geom_text(angle = 0, hjust = -0.2, size = 4) +
    facet_wrap(~ sample, scales = "free", ncol = 1) + # facet_grid(~ Month, scales = "free", space = "free")
    scale_y_discrete(position="right") +
    scale_fill_discrete(guide="none") + # eliminar las etiquetas del fill, si se quita fill eliminar esta linea 
    gggenes::theme_genes() +
    theme(panel.background = element_rect(fill = 'white' , color = 'white' ),
          panel.grid.major.y = ggplot2::element_line(colour = "grey", linewidth = 0.6),
          axis.title.y= element_blank(), 
          axis.text.y = element_text(size = 14, # sample name size
                                     family="Times New Roman", 
                                     face="bold"),
          plot.margin = unit(c(t=0.3, b=0.3, r=0.3, l=0.001), "mm") ##### posible error 
    )

所以,我想添加连接每个基因的线

第二个情节:

ggplot(data=df, aes(x=position,y=sample)) +
    geom_line(aes(group = gene), color= "grey", linetype="dashed", linewidth=0.3) + #  group = position
    gggenes::geom_gene_arrow(aes(xmin = start, xmax = end, y = sample, fill=gene),
                             arrowhead_width = grid::unit(7, "mm"), 
                             arrowhead_height = grid::unit(7, "mm"), 
                             arrow_body_height = grid::unit(5.7, "mm"), size=0.4 ) + 
    #  facet_wrap(~ sample, scales = "free", ncol = 1) +
    scale_y_discrete(position="right") +
    scale_fill_discrete(guide="none") + # eliminar las etiquetas del fill, si se quita fill eliminar esta linea 
    gggenes::theme_genes() +
    theme(panel.background = element_rect(fill = 'white' , color = 'white' ),
          panel.grid.major.y = ggplot2::element_line(colour = "grey", linewidth = 0.6),
          axis.title.y= element_blank(), 
          axis.text.y = element_text(size = 14, # sample name size
                                     family="Times New Roman", 
                                     face="bold"),
          plot.margin = unit(c(t=0.3, b=0.3, r=0.3, l=0.001), "mm") ##### posible error 
    ) 

我的代码中的问题是,如果我使用facet_wrap不生成行,有什么建议???

我只想将所有基因与第一个图类似地对齐,并将基因与与第二个图类似的线连接起来!!

r ggplot2 bioinformatics
1个回答
0
投票

如果您关闭剪切并创建一些“虚拟”数据,您可以绘制从一个面到另一个面的线,例如

library(tidyverse)
library(gggenes)

df <- read.table(text = "      sample       contig  start    end         qseqid  gene       cluster position
1 Genome-300 Genome-300_1 199743 201911 IMEHDJCA_00189 geneE cluster_00365 200827.0
2 Genome-300 Genome-300_1 201914 203275 IMEHDJCA_00190 geneD cluster_01313 202594.5
3 Genome-300 Genome-300_1 203272 205377 IMEHDJCA_00191 geneB cluster_00403 204324.5
4 Genome-300 Genome-300_1 206268 206663 IMEHDJCA_00193 geneC cluster_05858 206465.5
5 Genome-300 Genome-300_1 206686 222306 IMEHDJCA_00194 geneA cluster_00001 214496.0
6 Genome-306 Genome-306_1 287568 289736 DIBHEKPI_00259 geneE cluster_00365 288652.0")


ggplot(df, aes(xmin = start, xmax = end, x = position, y = sample, fill = gene, label = gene)) +
  geom_gene_arrow(arrow_body_height = unit(7, "mm"), 
                  arrowhead_height = unit(7, "mm"), 
                  arrowhead_width = unit(5.7, "mm")) +
  geom_text(angle = 0, hjust = -0.2, size = 4) +
  scale_y_discrete(position="right") +
  scale_fill_discrete(guide="none") +
  gggenes::theme_genes() +
  theme(panel.background = element_rect(fill = 'white' , color = 'white' ),
        panel.grid.major.y = ggplot2::element_line(colour = "grey", linewidth = 0.6),
        axis.title.y= element_blank(), 
        axis.text.y = element_text(size = 14,
                                   family="Times New Roman", 
                                   face="bold"),
        plot.margin = unit(c(t=0.3, b=0.3, r=0.3, l=0.001), "mm"),
        strip.background = element_blank(),
        strip.placement = "outside"
  ) +
  facet_wrap(~ sample, scales = "free", ncol = 1, strip.position = "top") +
  coord_cartesian(clip = 'off') +
  geom_segment(data = data.frame(sample = "Genome-306"),
               x = df %>% filter(sample == "Genome-306") %>% select(start) %>% min(),
               xend = df %>% filter(sample == "Genome-306") %>% select(end) %>% max(),
               y=1, yend=2.33, inherit.aes = FALSE, lty = 2)

创建于 2024-05-16,使用 reprex v2.1.0

您需要根据“真实”数据的绘图坐标调整线条的 y 位置。这是你想要的结果吗?

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