《Modern Statistics for Modern Biology》Chapter 一: 離散數(shù)據(jù)模型的預測(1.1 - 1.3)
《Modern Statistics for Modern Biology》Chapter 一: 離散數(shù)據(jù)模型的預測(1.4 - 1.5)
《Modern Statistics for Modern Biology》Chapter 二: 統(tǒng)計建模(2.1-2.3)
《Modern Statistics for Modern Biology》Chapter 二: 統(tǒng)計建模(2.4-2.5)
《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計建模(2.6 - 2.7)
《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計建模(2.8 - 2.9)
《Modern Statistics for Modern Biology》Chapter 二 統(tǒng)計建模(2.10 完結)
《Modern Statistics for Modern Biology》Chapter 三:R語言中的高質量圖形(3.1-3.4)
從這章開始最開始記錄一些markdown等小知識
$\hat{p}=\frac{1}{12}$
:
掌握R語言中的apply函數(shù)族
卡方檢驗
Hardy-Weinberg equilibrium( 哈迪-溫伯格平衡 )
帶你理解beta分布
簡單介紹一下R中的幾種統(tǒng)計分布及常用模型
- 統(tǒng)計分布每一種分布有四個函數(shù):
d――density(密度函數(shù))汹族,p――分布函數(shù),q――分位數(shù)函數(shù),r――隨機數(shù)函數(shù)
淮腾。比如柬采,正態(tài)分布的這四個函數(shù)為dnorm录煤,pnorm典尾,qnorm墨技,rnorm
惩阶。下面我們列出各分布后綴,前面加前綴d扣汪、p断楷、q或r就構成函數(shù)名:norm:正態(tài)
,t:t分布
崭别,f:F分布
冬筒,chisq:卡方
(包括非中心)unif:均勻
,exp:指數(shù)
茅主,weibull:威布爾舞痰,gamma:伽瑪
,beta:貝塔
lnorm:對數(shù)正態(tài)诀姚,logis:邏輯分布响牛,cauchy:柯西
,binom:二項分布
赫段,geom:幾何分布
呀打,hyper:超幾何
,nbinom:負二項
糯笙,pois:泊松
signrank:符號秩贬丛,wilcox:秩和
,tukey:學生化極差
如何預測一條序列是否含有CpG島
圖片輸出盡量保存為矢量圖
結合setNames
函數(shù)與scale_fill_manual
函數(shù)來指定ggplot2
的填充顏色(Figure 3.13)
要善于用stat_summary
來自定義函數(shù)結合ggplot2
進行繪圖
3.5 圖形語法
ggplot2的圖形語法的組成部分是
- 一個或者多個數(shù)據(jù)集
- 作為數(shù)據(jù)的可視表示的一個或多個幾何對象给涕,例如豺憔,點、直線够庙、矩形恭应、等高線
- 描述如何將數(shù)據(jù)中的變量映射到幾何對象的視覺特性(美學)和相關的比例(例如:線性,對數(shù)耘眨,秩)
- 一個或多個坐標系
- 統(tǒng)計總結規(guī)則
- 一種分面規(guī)范暮屡,即使用多個相似的子圖來查看相同數(shù)據(jù)的子集。
- 影響布局和渲染的可選參數(shù)毅桃,如文本大小褒纲、字體和對齊方式、圖例位置钥飞。
下面的代碼針對相同的數(shù)據(jù)在同一圖形中使用三種類型的幾何對象:點莺掠、直線和置信帶。
> 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 表示不添加圖例
- 如果我們想找出哪些基因是這些探針標識符的目標读宙,以及它們可能做什么彻秆,我們可以這樣做:
> 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在默認情況下使用stat=“smooth”
并顯示一條線结闸;如果使用geom_histogram
唇兑,則對數(shù)據(jù)進行分bin
,并以barplot
格式顯示結果桦锄。這里有一個例子:
> dfx = as.data.frame(Biobase::exprs(x))
> ggplot(dfx, aes(x = `20 E3.25`)) + geom_histogram(binwidth = 0.2)
? 問題
-
dfx
anddftx
有什么不同扎附?為什么要創(chuàng)建兩個?
我們回到之前的
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)建了一個繪圖對象结耀,如果我們要去展現(xiàn)
pb
, 會得到這是一個空白的圖形留夜。因為我們沒有指定任何特定的j幾何對象。到目前為止图甜,我們在pb
對象中所擁有的只是數(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)) # 設置x坐標標簽黑毅,旋轉90°
> pb
> pb = pb + scale_fill_manual(values = groupColor, name = "Groups") ## 指定填充顏色嚼摩,注意要與sampleGroup一致
Error in rlang::is_missing(values) : 找不到對象'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"
- 將柱狀圖轉換為極坐標圖
> 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
請注意,我們可以通過簡單地將它們設置為新值來覆蓋以前設置的主題參數(shù) - 無需返回重新創(chuàng)建pb矿瘦,我們最初設置它們
3.6 一維數(shù)據(jù)的可視化
- 生物數(shù)據(jù)分析的一個共同的任務是比較幾個樣本的單變量測量枕面。在本節(jié)中,我們將探索可視化和比較此類示例的一些可能性匪凡。作為示例膊畴,我們將使用四個基因
Fgf4
、Gata4
病游、GATA6
和Sox2
的強度唇跨。
> selectedProbes = c( Fgf4 = "1420085_at", Gata4 = "1418863_at",
+ Gata6 = "1425463_at", Sox2 = "1416967_at")
- 使用
reshape2
包中的melt
函數(shù),提取四個基因在總的數(shù)據(jù)中對應的信息衬衬。
> 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
- 為了更好地衡量买猖,我們還添加了一個列,該列提供了基因符號和探針標識符滋尉。
> 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中我們呈現(xiàn)了各個基因的均值來繪制條形圖玉控。然而采用均值來繪制會使我們丟失掉很多信息∈ㄏВ考慮到所需的空間量高诺,條形圖往往是一種可視化數(shù)據(jù)的糟糕方式碌识。
- 有時候我們想要添加
誤差線
,實現(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)
- 這里我們使用了兩個統(tǒng)計函數(shù)
mean
和mean_cl_normal
以及兩個幾何對象bar
和errorbar
虱而。mean_cl_normal
函數(shù)來源于Hmisc 包——用來計算平均值的標準誤差(或者置信區(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): 經驗累積分布函數(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ù)集中的一個數(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)