美文网首页生物信息编程R语言R语言可视化
欧剑虹老师BOOK学习记录:第一章 R/Bioconductor

欧剑虹老师BOOK学习记录:第一章 R/Bioconductor

作者: 热衷组培的二货潜 | 来源:发表于2019-05-04 16:34 被阅读29次

    首先最重要的参考链接:

    第一章 R/Bioconductor入门

    image.png

    啊啊啊,如有侵犯版权, 麻烦请私信我,看到立马删除!

    主要用来记录自己可能要用的一些知识点。(基本复制粘贴,建议直达链接)

    欧剑虹老师BOOK学习记录:第一章 R/Bioconductor入门(1)
    欧剑虹老师BOOK学习记录:第一章 R/Bioconductor入门(2):生物字符串 Biological strings

    这一节可以学到,外加一句:基础函数强大超乎你的想象!!!

    • 如何保存一个图像?
    • 如何绘制散点图?
    • 如何绘制饼图?
    • 如何绘制箱线图?
    • 如何绘制线图?
    • 如何绘制柱状图?

    保存图像

    • 格式
    XXX("xxx.XXX")
    绘图代码
    dev.off()
    
    ## 保存为PDF文件(好喜欢这种类型的总结,自己又懒得去总结)
    pdf("output.pdf"); plot(wt, mpg); dev.off()
    
    ## 保存为WMF文件
    win.metafile("output.wmf"); plot(wt, mpg); dev.off()
    
    ## 保存为PNG文件
    png("output.png"); plot(wt, mpg); dev.off()
    
    ## 保存为JPEG文件
    jpeg("output.jpg"); plot(wt, mpg); dev.off()
    
    ## 保存为BMP文件
    bmp("output.bmp"); plot(wt, mpg); dev.off()
    
    ## 保存为PS文件
    postscript("output.ps"); plot(wt, mpg); dev.off()
    

    散点图和趋势图

    • 一维散点图:dotchart
    • 看到这个图我想到了GO富集分析等涉及分类的可视化中的图完全可以采用此包
    > head(mtcars)
                       mpg cyl disp  hp drat    wt  qsec vs am gear carb
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
    
    > x <- mtcars[order(mtcars$mpg),]  # 将数据按照mpg列数据大小来排序
    
    > x$cyl <- factor(x$cyl) # cyl转换成factor
    
    # 这一段也可以使用ifelse以及tidyverse包中的case_when函数指定,推荐后者
    > x$color[x$cyl==4] <- "red"  # 指定cyl为4时,颜色用红色
    > x$color[x$cyl==6] <- "blue" # 指定cyl为6时,颜色用蓝色
    > x$color[x$cyl==8] <- "darkgreen"  # # 指定cyl为4时,颜色用墨绿色
    
    > dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
    +         main="Gas Milage for Car Models\ngrouped by cylinder",
    +      xlab="Miles Per Gallon", gcolor="black", color=x$color)
    
    顺带放一个参考了万年了的被我收藏在QQ表情图中的case_when实例, 自行比较一下 ifelsecase_when的区别。
    • 按汽缸数分组不同车型油耗
    • 二维散点图使用plot函数。直趋势线使用abline函数,拟合曲线在拟合后使用line函数绘制。
    > attach(mtcars)
    > plot(wt, mpg, main="Scatterplot Example", 
    +       xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
    
    # 添加拟合曲线
    # http://www.360doc.com/content/13/0418/08/11922517_279125397.shtml (R语言曲线拟合函数)
    > abline(lm(mpg~wt), col="red") # 渐近线 (y~x) # 线性回归方法
    > lines(lowess(wt,mpg), col="blue") # lowess line (x,y) # 局部回归的方法
    
    • 曲线使用lines函数。其type参数可以使用"p", "l", "o", "b", "c", "s", "S", "h", "n"等。
    • 通过for循环来批量绘图, 有时候明明你都会,但是思维很重要
    > x <- c(1:5); y <- x
    > opar <- par(pch=22, col="blue") # 使用蓝色绘制散点 
    > par(mfrow=c(2,4)) # 将画布分割成两行四列
    > opts <- c("p","l","o","b","c","s","S","h") 
    > for(i in 1:length(opts)){ 
    +    heading <- paste("type=", opts[i], sep="") 
    +    plot(x, y, main=heading) 
    +    lines(x, y, type=opts[i]) 
    + }
    
    > par(opar)
    

    注意!: R语言笔记--par()函数详解
    • 通过par( )函数对图形参数进行设置后,当需要还原为系统默认值时又该如何做呢?有两种方式可实现:
      • 一是在调用par()函数设置图形参数之前先执行op <-par(no.readonly=TURE)保存系统当前的环境,待需要还原时执行par(opar)即可;
      • 二就是直接关闭图形对话框,下次重新打开时即为默认设置。

    柱状图

    • 普通的柱状图使用barplot函数。其参数horiz=TRUE表示水平画图,beside=TRUE表示如果是多组数据的话,并排画图,否则原位堆叠画图。
    > op <- par(mfrow=c(1,2)) # 创建一个一行两列的画布
    > counts <- table(mtcars$vs, mtcars$gear) # 使用table函数进行计数
    
    > barplot(counts, main="Car Distribution by Gears and VS",
    +         xlab="Number of Gears", col=c("darkblue","red"),
    +         legend = rownames(counts),horiz=TRUE)
    
    > barplot(counts, main="Car Distribution by Gears and VS",
    +         xlab="Number of Gears", col=c("darkblue","red"),
    +         legend = rownames(counts), beside=TRUE)
    
    > par(op)
    
    • 柱状统计图使用hist函数。其breaks参数设置每组的范围,比如1:5。可以使用loess回归后计算出一条拟合曲线。对于hist图像,还可以使用density函数拟合曲线。
    > h <- hist(mtcars$mpg, breaks=12)
    > h
    $`breaks`
     [1] 10 12 14 16 18 20 22 24 26 28 30 32 34
    
    $counts
     [1] 2 1 7 3 5 5 2 2 1 0 2 2
    
    $density
     [1] 0.031250 0.015625 0.109375 0.046875 0.078125 0.078125 0.031250 0.031250 0.015625 0.000000 0.031250 0.031250
    
    $mids
     [1] 11 13 15 17 19 21 23 25 27 29 31 33
    
    $xname
    [1] "mtcars$mpg"
    
    $equidist
    [1] TRUE
    
    attr(,"class")
    [1] "histogram"
    
    > lo <- loess(h$counts~h$mids)
    > x<- seq(min(h$breaks), max(h$breaks), (max(h$breaks)-min(h$breaks))/1000)
    # 表示之前的breaks指定的从最小值到最大值分成1000个bin。
    
    > lines(x, predict(lo, x), col="red")
    > dens<-density(mtcars$mpg) # mpg的分布密度
    > dens
    
    Call:
            density.default(x = mtcars$mpg)
    
    Data: mtcars$mpg (32 obs.);     Bandwidth 'bw' = 2.477
    
           x               y            
     Min.   : 2.97   Min.   :6.481e-05  
     1st Qu.:12.56   1st Qu.:5.461e-03  
     Median :22.15   Median :1.926e-02  
     Mean   :22.15   Mean   :2.604e-02  
     3rd Qu.:31.74   3rd Qu.:4.530e-02  
     Max.   :41.33   Max.   :6.795e-02  
    
    > t <- dens$n  * length(dens$x) / sum(h$counts) / length(h$counts) / sum(h$density) # 这里没咋看懂。。。
    # dens$n 32
    # length(dens$x)  512
    # sum(h$counts) 32 表示mpg中统计的数值的个数
    # length(h$counts) 12 表示区间的个数
    # sum(h$density) 0.5 
    
    
    > lines(dens$x,dens$y*t,col="blue")
    
    

    饼图: 直接使用pie函数即可

    > x<-table(mtcars$gear)
    > pie(x,label=paste("gear=",rownames(x),sep=""))
    

    箱线图: boxplot函数

    • 绘图之前需要了解的一些符号
    • markdown表格格式:表格
    | 左对齐标题 | 右对齐标题 | 居中对齐标题 |
    | :------| ------: | :------: |
    | 短文本 | 中等文本 | 稍微长一点的文本 |
    | 稍微长一点的文本 | 短文本 | 中等文本 |
    
    :--- 代表左对齐
    :--: 代表居中对齐
    ---: 代表右对齐
    
    符号 示例 意义
    + +x 包括该变量
    - -x 不包括该变量
    : x:z 包括两变量的相互关系
    * x*z 包括两变量,以及它们之间的相互关系
    / x/z nesting: include z nested within x
    | x|z 条件或分组:包括指定z的x
    1 -1 截距:减去该截距
    > op <- par(mfrow=c(1,2))
    
    > boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", 
    +         xlab="Number of Cylinders", ylab="Miles Per Gallon")
    
    > head(ToothGrowth)
       len supp dose
    1  4.2   VC  0.5
    2 11.5   VC  0.5
    3  7.3   VC  0.5
    4  5.8   VC  0.5
    5  6.4   VC  0.5
    6 10.0   VC  0.5
    
    > boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE, 
    +         col=(c("gold","darkgreen")),
    +         main="Tooth Growth", xlab="Suppliment and Dose")
    > par(op)
    

    坐标轴,图例,标记与标题

    • 以上基础图像的坐标轴,图例,标记与标题等,大多可以通过下列参数来控制:
    参数 描述
    main 主标题
    sub 副标题
    xlab x轴标记
    ylab y轴标记
    xlim x轴上下限(范围)
    ylim y轴上下限
    • 增加一个新的坐标轴使用axis()函数。
    参数 描述
    side 坐标轴所在的位置,1:下,2:左,3:上,4:右
    at 坐标轴具体位置,通常由自动给出。
    labels 坐标字符串
    pos 坐标轴线所在的行,默认值为重绘所在位置上的原坐标
    lty 线型
    col 颜色
    las 坐标标记与坐标轴方位关系,=0为平等,=2为垂直
    lwd.ticks 坐标刻度线宽度
    col.ticks 坐标刻度线颜色
    • 绘制双纵坐标图
      • yaxt: 是否显示y轴坐标值;默认显示,值为'n'时,不显示
      • las :标签是否平行于(=0)或垂直于(=2)坐标轴
      • side 坐标轴所在的位置,1:下, 2:左, 3:上, 4:右
    > x <- c(1:10); y <- x; z <- 10/x
    
    > op <- par(mar=c(5, 4, 4, 8)+ .1) ##右侧多留点空
    
    > plot(x, y,type="b", pch=21, col="red", 
    +      yaxt="n", lty=3, xlab="", ylab="") ##设置不画纵坐标
    
    > lines(x, z, type="b", pch=22, col="blue", lty=2)
    > axis(2, at=x,labels=x, col.axis="red", las=2) ##绘制左侧纵坐标
    
    > axis(4, at=z,labels=round(z,digits=2),
    +      col.axis="blue", las=2, cex.axis=0.7, tck=-.01)##绘制右侧坐标
    
    > mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue") ##右侧坐标加标注
    
    > title("An Example of Creative Axes", xlab="X values",
    +       ylab="Y=X") ##为图像加主标题
    > par(op)
    
    • 绘制有细节的坐标轴
    > plot(x, y, yaxt="n")
    > majorat <- seq(0, 10, by=2)
    > majorlab <- majorat
    > axis(2, at=majorat, labels=majorlab)
    > minorat <- seq(0, 10, by=.4)
    > minorat <- minorat[!minorat %in% majorat]
    > axis(2, at=minorat, tcl=par("tcl")*.5, label=FALSE, lwd=0, lwd.ticks =1)
    

    图例使用legend()函数控制

    • 图例所在位置,可以使用bottom, bottomleft, left, topleft, top, topright, right, bottomleft, center来指定。
    • inset 设置在主绘图边距
    • title 图例的标题
    • legend 图例的内容
    > boxplot(mpg~cyl, main="Milage by Car Weight",
    +           yaxt="n", xlab="Milage", horizontal=TRUE,
    +           col=terrain.colors(3))
    
    > legend("topright", inset=.05, title="Number of Cylinders",
    +          c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)
    

    基础绘图很强大 !!!

    相关文章

      网友评论

        本文标题:欧剑虹老师BOOK学习记录:第一章 R/Bioconductor

        本文链接:https://www.haomeiwen.com/subject/dyvxoqtx.html