跟著Nature Genetics 學(xué)畫(huà)圖:R語(yǔ)言ggplot2畫(huà)基因結(jié)構(gòu)示意圖

今天推文重復(fù)的圖來(lái)自于 論文
Whole-genome resequencing of 445 Lactuca accessions reveals the domestication history of cultivated lettuce

image.png

今天試著重復(fù)的圖片對(duì)應(yīng)著的是論文附件中的Figure8c,基因結(jié)構(gòu)圖,論文中文字部分對(duì)圖的描述是 Gene structure of Lsat_6X11620. Closed bars represent exons, and open bars represent untranslated regions and introns. The positions of the SNPs in the promoter region are indicated by black triangles. An highly associated SNP, A-to-G transition at Chr. 6:15,542,968 is represented by a red triangle.

image.png

首先是準(zhǔn)備數(shù)據(jù)

表示整個(gè)基因的矩形數(shù)據(jù)
gene1<-data.frame(
  xmin=15000,
  xmax=16000,
  ymin=1,
  ymax=2
)
外顯子的數(shù)據(jù)
exon<-
  data.frame(
    xmin=c(15100,15300,15700),
    xmax=c(15200,15600,15900),
    ymin=1,
    ymax=2,
    label=paste0("exon_",1:3)
  )
基因上下游的線段的數(shù)據(jù)
df<-data.frame(
  x=14500,
  xend=16500,
  y=1.5,
  yend=1.5
)
snp的位置數(shù)據(jù)
df1<-
  data.frame(
    x=c(14510,14530,14560,14590),
    y=1.5
  )
df2<-
  data.frame(
    x=c(14520),
    y=1.5
  )
畫(huà)圖代碼
library(ggplot2)
library(ggfittext)
ggplot()+
  geom_segment(data=df,aes(
    x=x,xend=xend,y=y,yend=yend
  ))+
  geom_rect(data=gene1,aes(xmin=xmin,
                           xmax=xmax,
                           ymin=ymin,
                           ymax=ymax),
            fill="white",color="black")+
  geom_rect(data=exon,aes(xmin=xmin,
                          xmax=xmax,
                          ymin=ymin,
                          ymax=ymax),
            color="black")+
  geom_fit_text(data=exon,aes(xmin=xmin,
                              xmax=xmax,
                              ymin=ymin,
                              ymax=ymax,
                              label=label),
                contrast = TRUE)+
  geom_point(data=df1,aes(x=x,y=y-0.05),
             shape=17)+
  geom_point(data = df2,aes(x=x,y=y+0.05),
             shape=25,fill="red",color="red")+
  theme_minimal()+
  theme(aspect.ratio = 0.2,
        panel.grid = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        axis.line.x=element_line(),
        axis.ticks.x=element_line())+
  scale_x_continuous(labels = c("1.45",
                                "1.50",
                                "1.55",
                                "1.60",
                                "1.65"))+
  labs(x="Chromosome 6 (MB)")

最終結(jié)果如下

image.png

這個(gè)地方新遇到了一個(gè)R包是ggfittext,github對(duì)應(yīng)的鏈接是 https://github.com/wilkox/ggfittext

這個(gè)包的主要作用是可以讓指定區(qū)域內(nèi)的文字自動(dòng)適應(yīng)區(qū)域的大小,背景等,還可以根據(jù)區(qū)域自動(dòng)換行等等,后面爭(zhēng)取出一期推文專門(mén)介紹這個(gè)包

另外之前的推文遇到了一個(gè)問(wèn)題是ggplot2添加文本標(biāo)簽的時(shí)候如何讓文字居左或者居右顯示,有人留言說(shuō)是hjust參數(shù),我原來(lái)一直以為這個(gè)參數(shù)是讓文本左右移動(dòng),原來(lái)hjust設(shè)置為0.5,就是居中,設(shè)置為1就是居右,設(shè)置為0就是居左

本篇推文的完整代碼

library(ggplot2)
library(ggfittext)
gene1 <- data.frame(
  xmin = 15000,
  xmax = 16000,
  ymin = 1,
  ymax = 2
)
exon <-
  data.frame(
    xmin = c(15100, 15300, 15700),
    xmax = c(15200, 15600, 15900),
    ymin = 1,
    ymax = 2,
    label = paste0("exon_", 1:3)
  )
df <- data.frame(
  x = 14500,
  xend = 16500,
  y = 1.5,
  yend = 1.5
)
df1 <-
  data.frame(
    x = c(14510, 14530, 14560, 14590),
    y = 1.5
  )
df2 <-
  data.frame(
    x = c(14520),
    y = 1.5
  )

ggplot() +
  geom_segment(data = df, aes(
    x = x, xend = xend, y = y, yend = yend
  )) +
  geom_rect(
    data = gene1, aes(
      xmin = xmin,
      xmax = xmax,
      ymin = ymin,
      ymax = ymax
    ),
    fill = "white", color = "black"
  ) +
  geom_rect(
    data = exon, aes(
      xmin = xmin,
      xmax = xmax,
      ymin = ymin,
      ymax = ymax
    ),
    color = "black"
  ) +
  geom_fit_text(
    data = exon, aes(
      xmin = xmin,
      xmax = xmax,
      ymin = ymin,
      ymax = ymax,
      label = label
    ),
    contrast = TRUE
  ) +
  geom_point(
    data = df1, aes(x = x, y = y - 0.05),
    shape = 17
  ) +
  geom_point(
    data = df2, aes(x = x, y = y + 0.05),
    shape = 25, fill = "red", color = "red"
  ) +
  theme_minimal() +
  theme(
    aspect.ratio = 0.2,
    panel.grid = element_blank(),
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.line.x = element_line(),
    axis.ticks.x = element_line()
  ) +
  scale_x_continuous(labels = c(
    "1.45",
    "1.50",
    "1.55",
    "1.60",
    "1.65"
  )) +
  labs(x = "Chromosome 6 (MB)")

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門(mén)學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容