数据-R语言-图表-决策-Linux-PythonR小推车

《Modern Statistics for Modern Bi

2019-03-11  本文已影响44人  热衷组培的二货潜

《Modern Statistics for Modern Biology》Chapter 一: 离散数据模型的预测(1.1 - 1.3)

《Modern Statistics for Modern Biology》Chapter 一: 离散数据模型的预测(1.4 - 1.5)

《Modern Statistics for Modern Biology》Chapter 二: 统计建模(2.1-2.3)

《Modern Statistics for Modern Biology》Chapter 二: 统计建模(2.4-2.5)

《Modern Statistics for Modern Biology》Chapter 二 统计建模(2.6 - 2.7)

《Modern Statistics for Modern Biology》Chapter 二 统计建模(2.8 - 2.9)

《Modern Statistics for Modern Biology》Chapter 二 统计建模(2.10 完结)

《Modern Statistics for Modern Biology》Chapter 三:R语言中的高质量图形(3.1-3.4)

从这章开始最开始记录一些markdown等小知识
$\hat{p}=\frac{1}{12}$\hat{p}=\frac{1}{12}
掌握R语言中的apply函数族
卡方检验
Hardy-Weinberg equilibrium( 哈迪-温伯格平衡 )
带你理解beta分布
简单介绍一下R中的几种统计分布及常用模型

  • 统计分布每一种分布有四个函数:d――density(密度函数),p――分布函数,q――分位数函数,r――随机数函数。比如,正态分布的这四个函数为dnorm,pnorm,qnorm,rnorm。下面我们列出各分布后缀,前面加前缀d、p、q或r就构成函数名:norm:正态t:t分布f:F分布chisq:卡方(包括非中心) unif:均匀exp:指数,weibull:威布尔,gamma:伽玛beta:贝塔 lnorm:对数正态,logis:逻辑分布,cauchy:柯西binom:二项分布geom:几何分布hyper:超几何nbinom:负二项pois:泊松 signrank:符号秩,wilcox:秩和tukey:学生化极差
    如何预测一条序列是否含有CpG岛
    图片输出尽量保存为矢量图
    结合setNames函数与scale_fill_manual函数来指定ggplot2的填充颜色(Figure 3.13)
    要善于用stat_summary来自定义函数结合ggplot2进行绘图

3.5 图形语法

ggplot2的图形语法的组成部分是


下面的代码针对相同的数据在同一图形中使用三种类型的几何对象:点、直线和置信带。

> 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
> dfx = as.data.frame(Biobase::exprs(x))
> ggplot(dfx, aes(x = `20 E3.25`)) + geom_histogram(binwidth = 0.2)

问题

> 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))
> class(pb)
[1] "gg"     "ggplot"
> pb
> 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

请注意,我们可以通过简单地将它们设置为新值来覆盖以前设置的主题参数 - 无需返回重新创建pb,我们最初设置它们

3.6 一维数据的可视化

> selectedProbes = c( Fgf4 = "1420085_at", Gata4 = "1418863_at",
+                    Gata6 = "1425463_at",  Sox2 = "1416967_at")
> 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
> ggplot(genes, aes( x = gene, y = value)) +
+   stat_summary(fun.y = mean, geom = "bar")
Figure 3.15
> 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

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

> 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

> 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)
上一篇 下一篇

猜你喜欢

热点阅读