《Modern Statistics for Modern Biology》Chapter 三:R語(yǔ)言中的高質(zhì)量圖形(3.5-3.6)

《Modern Statistics for Modern Biology》Chapter 一: 離散數(shù)據(jù)模型的預(yù)測(cè)(1.1 - 1.3)

《Modern Statistics for Modern Biology》Chapter 一: 離散數(shù)據(jù)模型的預(yù)測(cè)(1.4 - 1.5)

《Modern Statistics for Modern Biology》Chapter 二: 統(tǒng)計(jì)建模(2.1-2.3)

《Modern Statistics for Modern Biology》Chapter 二: 統(tǒng)計(jì)建模(2.4-2.5)

《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計(jì)建模(2.6 - 2.7)

《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計(jì)建模(2.8 - 2.9)

《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計(jì)建模(2.10 完結(jié))

《Modern Statistics for Modern Biology》Chapter 三:R語(yǔ)言中的高質(zhì)量圖形(3.1-3.4)

從這章開始最開始記錄一些markdown等小知識(shí)
$\hat{p}=\frac{1}{12}$\hat{p}=\frac{1}{12}
掌握R語(yǔ)言中的apply函數(shù)族
卡方檢驗(yàn)
Hardy-Weinberg equilibrium( 哈迪-溫伯格平衡 )
帶你理解beta分布
簡(jiǎn)單介紹一下R中的幾種統(tǒng)計(jì)分布及常用模型

  • 統(tǒng)計(jì)分布每一種分布有四個(gè)函數(shù):d――density(密度函數(shù)),p――分布函數(shù),q――分位數(shù)函數(shù),r――隨機(jī)數(shù)函數(shù)。比如,正態(tài)分布的這四個(gè)函數(shù)為dnorm,pnorm,qnorm,rnorm。下面我們列出各分布后綴,前面加前綴d、p、q或r就構(gòu)成函數(shù)名:norm:正態(tài),t:t分布,f:F分布,chisq:卡方(包括非中心) unif:均勻,exp:指數(shù),weibull:威布爾,gamma:伽瑪,beta:貝塔 lnorm:對(duì)數(shù)正態(tài),logis:邏輯分布,cauchy:柯西, binom:二項(xiàng)分布,geom:幾何分布hyper:超幾何,nbinom:負(fù)二項(xiàng),pois:泊松 signrank:符號(hào)秩,wilcox:秩和,tukey:學(xué)生化極差
    如何預(yù)測(cè)一條序列是否含有CpG島
    圖片輸出盡量保存為矢量圖
    結(jié)合setNames函數(shù)與scale_fill_manual函數(shù)來指定ggplot2的填充顏色(Figure 3.13)
    要善于用stat_summary來自定義函數(shù)結(jié)合ggplot2進(jìn)行繪圖

3.5 圖形語(yǔ)法

ggplot2的圖形語(yǔ)法的組成部分是

  • 一個(gè)或者多個(gè)數(shù)據(jù)集
  • 作為數(shù)據(jù)的可視表示的一個(gè)或多個(gè)幾何對(duì)象,例如,點(diǎn)、直線、矩形、等高線
  • 描述如何將數(shù)據(jù)中的變量映射到幾何對(duì)象的視覺特性(美學(xué))和相關(guān)的比例(例如:線性,對(duì)數(shù),秩)
  • 一個(gè)或多個(gè)坐標(biāo)系
  • 統(tǒng)計(jì)總結(jié)規(guī)則
  • 一種分面規(guī)范,即使用多個(gè)相似的子圖來查看相同數(shù)據(jù)的子集。
  • 影響布局和渲染的可選參數(shù),如文本大小、字體和對(duì)齊方式、圖例位置。

下面的代碼針對(duì)相同的數(shù)據(jù)在同一圖形中使用三種類型的幾何對(duì)象:點(diǎn)、直線和置信帶。

> BiocManager::install("Biobase", version = "3.8")
> library("Hiiragi2013")
> data("x")
> dim(Biobase::exprs(x))
> dftx = data.frame(t(Biobase::exprs(x)), pData(x))
> ggplot( dftx, aes( x = X1426642_at, y = X1418765_at)) +
  geom_point( shape = 1 ) +
  geom_smooth( method = "loess" )
ggplot( dftx, aes( x = X1426642_at, y = X1418765_at ))  +
  geom_point( aes( color = sampleColour), shape = 19 ) +
  geom_smooth( method = "loess" ) +
  scale_color_discrete( guide = FALSE ) ## guide = FALSE 表示不添加圖例
  • 如果我們想找出哪些基因是這些探針標(biāo)識(shí)符的目標(biāo),以及它們可能做什么,我們可以這樣做:
> library("mouse4302.db")
> AnnotationDbi::select(mouse4302.db,
+    keys = c("1426642_at", "1418765_at"), keytype = "PROBEID",
+    columns = c("SYMBOL", "GENENAME"))
'select()' returned 1:1 mapping between keys and columns
     PROBEID SYMBOL                                            GENENAME
1 1426642_at    Fn1                                       fibronectin 1
2 1418765_at  Timd2 T cell immunoglobulin and mucin domain containing 2
  • 如果使用的是geom_smooth,那么ggplot2在默認(rèn)情況下使用stat=“smooth”并顯示一條線;如果使用geom_histogram,則對(duì)數(shù)據(jù)進(jìn)行分bin,并以barplot格式顯示結(jié)果。這里有一個(gè)例子:
> dfx = as.data.frame(Biobase::exprs(x))
> ggplot(dfx, aes(x = `20 E3.25`)) + geom_histogram(binwidth = 0.2)

? 問題

  • dfx and dftx有什么不同?為什么要?jiǎng)?chuàng)建兩個(gè)?

  • 我們回到之前的barplot, 賦值給pb

> library("dplyr")
> groups = group_by(pData(x), sampleGroup) %>%
+   summarise(n = n(), color = unique(sampleColour))
> head(groups)
# A tibble: 6 x 3
  sampleGroup         n color  
  <chr>           <int> <chr>  
1 E3.25              36 #CAB2D6
2 E3.25 (FGF4-KO)    17 #FDBF6F
3 E3.5 (EPI)         11 #A6CEE3
4 E3.5 (FGF4-KO)      8 #FF7F00
5 E3.5 (PE)          11 #B2DF8A
6 E4.5 (EPI)          4 #1F78B4
> groupColor = setNames(groups$color, groups$sampleGroup)
> pb = ggplot(groups, aes(x = sampleGroup, y = n))
  • 我們創(chuàng)建了一個(gè)繪圖對(duì)象,如果我們要去展現(xiàn)pb, 會(huì)得到這是一個(gè)空白的圖形。因?yàn)槲覀儧]有指定任何特定的j幾何對(duì)象。到目前為止,我們?cè)?code>pb對(duì)象中所擁有的只是數(shù)據(jù)和aesthetics
> class(pb)
[1] "gg"     "ggplot"
> pb
  • 現(xiàn)在我們可以一層一層的添加圖層。
> pb = pb + geom_bar(stat = "identity") ## 柱狀圖
> pb
> pb = pb + aes(fill = sampleGroup) ## 按group填充顏色
> pb
> pb = pb + theme(axis.text.x = element_text(angle = 90, hjust = 1)) # 設(shè)置x坐標(biāo)標(biāo)簽,旋轉(zhuǎn)90°
> pb
> pb = pb + scale_fill_manual(values = groupColor, name = "Groups") ## 指定填充顏色,注意要與sampleGroup一致
Error in rlang::is_missing(values) : 找不到對(duì)象'groupColor'
> groupColor = setNames(groups$color, groups$sampleGroup)
> pb = pb + scale_fill_manual(values = groupColor, name = "Groups")
> pb
> groups
# A tibble: 8 x 3
  sampleGroup         n color  
  <chr>           <int> <chr>  
1 E3.25              36 #CAB2D6
2 E3.25 (FGF4-KO)    17 #FDBF6F
3 E3.5 (EPI)         11 #A6CEE3
4 E3.5 (FGF4-KO)      8 #FF7F00
5 E3.5 (PE)          11 #B2DF8A
6 E4.5 (EPI)          4 #1F78B4
7 E4.5 (FGF4-KO)     10 #E31A1C
8 E4.5 (PE)           4 #33A02C
> groupColor 
          E3.25 E3.25 (FGF4-KO)      E3.5 (EPI)  E3.5 (FGF4-KO)       E3.5 (PE)      E4.5 (EPI)  E4.5 (FGF4-KO)       E4.5 (PE) 
      "#CAB2D6"       "#FDBF6F"       "#A6CEE3"       "#FF7F00"       "#B2DF8A"       "#1F78B4"       "#E31A1C"       "#33A02C" 

  • 將柱狀圖轉(zhuǎn)換為極坐標(biāo)圖
> pb.polar = pb + coord_polar() +
+   theme(axis.text.x = element_text(angle = 0, hjust = 1),
+         axis.text.y = element_blank(),
+         axis.ticks = element_blank()) +
+   xlab("") + ylab("")
> polar


請(qǐng)注意,我們可以通過簡(jiǎn)單地將它們?cè)O(shè)置為新值來覆蓋以前設(shè)置的主題參數(shù) - 無需返回重新創(chuàng)建pb,我們最初設(shè)置它們

3.6 一維數(shù)據(jù)的可視化

  • 生物數(shù)據(jù)分析的一個(gè)共同的任務(wù)是比較幾個(gè)樣本的單變量測(cè)量。在本節(jié)中,我們將探索可視化和比較此類示例的一些可能性。作為示例,我們將使用四個(gè)基因Fgf4Gata4、GATA6Sox2的強(qiáng)度。
> selectedProbes = c( Fgf4 = "1420085_at", Gata4 = "1418863_at",
+                    Gata6 = "1425463_at",  Sox2 = "1416967_at")
  • 使用reshape2包中的melt函數(shù),提取四個(gè)基因在總的數(shù)據(jù)中對(duì)應(yīng)的信息。
> library("reshape2")
> genes = melt(Biobase::exprs(x)[selectedProbes, ],
+              varnames = c("probe", "sample"))
> head(genes)
       probe  sample    value
1 1420085_at 1 E3.25 3.027715
2 1418863_at 1 E3.25 4.843137
3 1425463_at 1 E3.25 5.500618
4 1416967_at 1 E3.25 1.731217
5 1420085_at 2 E3.25 9.293016
6 1418863_at 2 E3.25 5.530016
  • 為了更好地衡量,我們還添加了一個(gè)列,該列提供了基因符號(hào)和探針標(biāo)識(shí)符。
> genes$gene =
  names(selectedProbes)[match(genes$probe, selectedProbes)]
> head(genes)
       probe  sample    value  gene
1 1420085_at 1 E3.25 3.027715  Fgf4
2 1418863_at 1 E3.25 4.843137 Gata4
3 1425463_at 1 E3.25 5.500618 Gata6
4 1416967_at 1 E3.25 1.731217  Sox2
5 1420085_at 2 E3.25 9.293016  Fgf4
6 1418863_at 2 E3.25 5.530016 Gata4
  • 3.6.1 Barplot
> ggplot(genes, aes( x = gene, y = value)) +
+   stat_summary(fun.y = mean, geom = "bar")
Figure 3.15
  • Figure 3.15中我們呈現(xiàn)了各個(gè)基因的均值來繪制條形圖。然而采用均值來繪制會(huì)使我們丟失掉很多信息??紤]到所需的空間量,條形圖往往是一種可視化數(shù)據(jù)的糟糕方式。
  • 有時(shí)候我們想要添加誤差線,實(shí)現(xiàn)如下:
> library("Hmisc")
> ggplot(genes, aes( x = gene, y = value, fill = gene)) +
+   stat_summary(fun.y = mean, geom = "bar") +
+   stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
+                width = 0.25)
image.png
  • 這里我們使用了兩個(gè)統(tǒng)計(jì)函數(shù)meanmean_cl_normal以及兩個(gè)幾何對(duì)象barerrorbar。mean_cl_normal函數(shù)來源于Hmisc 包——用來計(jì)算平均值的標(biāo)準(zhǔn)誤差(或者置信區(qū)間)。

3.6.2 Boxplots

> p = ggplot(genes, aes( x = gene, y = value, fill = gene))
> p + geom_boxplot()


3.6.3 Violin plots

> p + geom_violin()


3.6.4 Dot plots and beeswarm plots

> p + geom_dotplot(binaxis = "y", binwidth = 1/6,
+        stackdir = "center", stackratio = 0.75,
+        aes(color = gene))

> library("ggbeeswarm")
> p + geom_beeswarm(aes(color = gene))

3.6.5 Density plots

> ggplot(genes, aes( x = value, color = gene)) + geom_density()

3.6.6 ECDF plots

  • cumulative distribution function (CDF): 累積分布函數(shù)
    F(x)=P(X≤x)
  • empirical cumulative distribution function (ECDF): 經(jīng)驗(yàn)累積分布函數(shù)
> simdata = rnorm(70)
> tibble(index = seq(along = simdata),
+           sx = sort(simdata)) %>%
+ ggplot(aes(x = sx, y = index)) + geom_step()
> ggplot(genes, aes( x = value, color = gene)) + stat_ecdf()

3.6.7 The effect of transformations on densities

  • 很容易觀察直方圖或密度圖,并檢查它們是否有雙峰(或多模態(tài))的證據(jù),以表明某些潛在的生物現(xiàn)象。在此之前,重要的是要記住,密度的模式的數(shù)量取決于數(shù)據(jù)的比例變換,通過鏈規(guī)則。例如,讓我們查看來自Hiiragi數(shù)據(jù)集中的一個(gè)數(shù)組的數(shù)據(jù)
> ggplot(dfx, aes(x = `64 E4.5 (EPI)`)) + geom_histogram(bins = 100)
> ggplot(dfx, aes(x = 2 ^ `64 E4.5 (EPI)`)) + 
+   geom_histogram(binwidth = 20) + xlim(0, 1500)
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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