首先最重要的參考鏈接:

image.png
啊啊啊,如有侵犯版權(quán), 麻煩請(qǐng)私信我,看到立馬刪除!
主要用來記錄自己可能要用的一些知識(shí)點(diǎn)。(基本復(fù)制粘貼,建議直達(dá)鏈接)
歐劍虹老師BOOK學(xué)習(xí)記錄:第一章 R/Bioconductor入門(1)
歐劍虹老師BOOK學(xué)習(xí)記錄:第一章 R/Bioconductor入門(2):生物字符串 Biological strings
這一節(jié)可以學(xué)到,外加一句:基礎(chǔ)函數(shù)強(qiáng)大超乎你的想象!!!
- 如何保存一個(gè)圖像?
- 如何繪制散點(diǎn)圖?
- 如何繪制餅圖?
- 如何繪制箱線圖?
- 如何繪制線圖?
- 如何繪制柱狀圖?
保存圖像
- 格式
XXX("xxx.XXX")
繪圖代碼
dev.off()
## 保存為PDF文件(好喜歡這種類型的總結(jié),自己又懶得去總結(jié))
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()
散點(diǎn)圖和趨勢(shì)圖
-
一維散點(diǎn)圖:
dotchart - 看到這個(gè)圖我想到了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),] # 將數(shù)據(jù)按照mpg列數(shù)據(jù)大小來排序
> x$cyl <- factor(x$cyl) # cyl轉(zhuǎn)換成factor
# 這一段也可以使用ifelse以及tidyverse包中的case_when函數(shù)指定,推薦后者
> x$color[x$cyl==4] <- "red" # 指定cyl為4時(shí),顏色用紅色
> x$color[x$cyl==6] <- "blue" # 指定cyl為6時(shí),顏色用藍(lán)色
> x$color[x$cyl==8] <- "darkgreen" # # 指定cyl為4時(shí),顏色用墨綠色
> 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)

順帶放一個(gè)參考了萬年了的被我收藏在QQ表情圖中的case_when實(shí)例, 自行比較一下 ifelse和case_when的區(qū)別。

- 按汽缸數(shù)分組不同車型油耗
- 二維散點(diǎn)圖使用plot函數(shù)。直趨勢(shì)線使用abline函數(shù),擬合曲線在擬合后使用line函數(shù)繪制。
> 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語言曲線擬合函數(shù))
> abline(lm(mpg~wt), col="red") # 漸近線 (y~x) # 線性回歸方法
> lines(lowess(wt,mpg), col="blue") # lowess line (x,y) # 局部回歸的方法

- 曲線使用lines函數(shù)。其type參數(shù)可以使用"p", "l", "o", "b", "c", "s", "S", "h", "n"等。
-
通過
for循環(huán)來批量繪圖,有時(shí)候明明你都會(huì),但是思維很重要。
> x <- c(1:5); y <- x
> opar <- par(pch=22, col="blue") # 使用藍(lán)色繪制散點(diǎn)
> 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()函數(shù)詳解
- 通過par( )函數(shù)對(duì)圖形參數(shù)進(jìn)行設(shè)置后,當(dāng)需要還原為系統(tǒng)默認(rèn)值時(shí)又該如何做呢?有兩種方式可實(shí)現(xiàn):
- 一是在調(diào)用
par()函數(shù)設(shè)置圖形參數(shù)之前先執(zhí)行op <-par(no.readonly=TURE)保存系統(tǒng)當(dāng)前的環(huán)境,待需要還原時(shí)執(zhí)行par(opar)即可; - 二就是
直接關(guān)閉圖形對(duì)話框,下次重新打開時(shí)即為默認(rèn)設(shè)置。
- 一是在調(diào)用
柱狀圖
- 普通的柱狀圖使用
barplot函數(shù)。其參數(shù)horiz=TRUE表示水平畫圖,beside=TRUE表示如果是多組數(shù)據(jù)的話,并排畫圖,否則原位堆疊畫圖。
> op <- par(mfrow=c(1,2)) # 創(chuàng)建一個(gè)一行兩列的畫布
> counts <- table(mtcars$vs, mtcars$gear) # 使用table函數(shù)進(jìn)行計(jì)數(shù)
> 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)

- 柱狀統(tǒng)計(jì)圖使用
hist函數(shù)。其breaks參數(shù)設(shè)置每組的范圍,比如1:5??梢允褂?code>loess回歸后計(jì)算出一條擬合曲線。對(duì)于hist圖像,還可以使用density函數(shù)擬合曲線。
> 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個(gè)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中統(tǒng)計(jì)的數(shù)值的個(gè)數(shù)
# length(h$counts) 12 表示區(qū)間的個(gè)數(shù)
# sum(h$density) 0.5
> lines(dens$x,dens$y*t,col="blue")

餅圖: 直接使用
pie函數(shù)即可
> x<-table(mtcars$gear)
> pie(x,label=paste("gear=",rownames(x),sep=""))

箱線圖:
boxplot函數(shù)
- 繪圖之前需要了解的一些符號(hào)
-
markdown表格格式:表格
| 左對(duì)齊標(biāo)題 | 右對(duì)齊標(biāo)題 | 居中對(duì)齊標(biāo)題 |
| :------| ------: | :------: |
| 短文本 | 中等文本 | 稍微長一點(diǎn)的文本 |
| 稍微長一點(diǎn)的文本 | 短文本 | 中等文本 |
:--- 代表左對(duì)齊
:--: 代表居中對(duì)齊
---: 代表右對(duì)齊
| 符號(hào) | 示例 | 意義 |
|---|---|---|
| + | +x | 包括該變量 |
| - | -x | 不包括該變量 |
| : | x:z | 包括兩變量的相互關(guān)系 |
| * | x*z | 包括兩變量,以及它們之間的相互關(guān)系 |
| / | 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)

坐標(biāo)軸,圖例,標(biāo)記與標(biāo)題
- 以上基礎(chǔ)圖像的坐標(biāo)軸,圖例,標(biāo)記與標(biāo)題等,大多可以通過下列參數(shù)來控制:
| 參數(shù) | 描述 |
|---|---|
| main | 主標(biāo)題 |
| sub | 副標(biāo)題 |
| xlab | x軸標(biāo)記 |
| ylab | y軸標(biāo)記 |
| xlim | x軸上下限(范圍) |
| ylim | y軸上下限 |
- 增加一個(gè)新的坐標(biāo)軸使用axis()函數(shù)。
| 參數(shù) | 描述 |
|---|---|
| side | 坐標(biāo)軸所在的位置,1:下,2:左,3:上,4:右 |
| at | 坐標(biāo)軸具體位置,通常由自動(dòng)給出。 |
| labels | 坐標(biāo)字符串 |
| pos | 坐標(biāo)軸線所在的行,默認(rèn)值為重繪所在位置上的原坐標(biāo) |
| lty | 線型 |
| col | 顏色 |
| las | 坐標(biāo)標(biāo)記與坐標(biāo)軸方位關(guān)系,=0為平等,=2為垂直 |
| lwd.ticks | 坐標(biāo)刻度線寬度 |
| col.ticks | 坐標(biāo)刻度線顏色 |
-
繪制雙縱坐標(biāo)圖
-
yaxt: 是否顯示y軸坐標(biāo)值;默認(rèn)顯示,值為'n'時(shí),不顯示 -
las:標(biāo)簽是否平行于(=0)或垂直于(=2)坐標(biāo)軸 -
side坐標(biāo)軸所在的位置,1:下,2:左,3:上,4:右
-
> x <- c(1:10); y <- x; z <- 10/x
> op <- par(mar=c(5, 4, 4, 8)+ .1) ##右側(cè)多留點(diǎn)空
> plot(x, y,type="b", pch=21, col="red",
+ yaxt="n", lty=3, xlab="", ylab="") ##設(shè)置不畫縱坐標(biāo)
> lines(x, z, type="b", pch=22, col="blue", lty=2)
> axis(2, at=x,labels=x, col.axis="red", las=2) ##繪制左側(cè)縱坐標(biāo)
> axis(4, at=z,labels=round(z,digits=2),
+ col.axis="blue", las=2, cex.axis=0.7, tck=-.01)##繪制右側(cè)坐標(biāo)
> mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue") ##右側(cè)坐標(biāo)加標(biāo)注
> title("An Example of Creative Axes", xlab="X values",
+ ylab="Y=X") ##為圖像加主標(biāo)題
> par(op)

- 繪制有細(xì)節(jié)的坐標(biāo)軸
> 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()函數(shù)控制
- 圖例所在位置,可以使用
bottom,bottomleft,left,topleft,top,topright,right,bottomleft,center來指定。 inset設(shè)置在主繪圖邊距title圖例的標(biāo)題legend圖例的內(nèi)容
> 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)
